当前位置: 首页 > news >正文

Mockito+PowerMock+Junit单元测试

一、单元测试用途

1、日常开发团队要求规范,需要对开发需求代码进行单元测试并要求行覆盖率达到要求,DevOps流水线也会开设相关门禁阀值阻断代码提交,一般新增代码行覆盖率80%左右。

二、Mock测试介绍

1、Mock是为了解决不同的单元之间由于耦合而难于开发、测试的问题。所以,Mock既能出现在单元测试中,也会出现在集成测试、系统测试过程中。Mock 最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。

2、Mock 测试就是在测试活动中,对于某些不容易构造或者不容易获取的比较复杂的数据/场景,用一个虚拟的对象(Mock对象)来创建用于测试的测试方法。

3、Mock重要作用

Mock是为了解决不同的单元之间由于耦合而难于开发、测试的问题。所以,Mock既能出现在单元测试中,也会出现在集成测试、系统测试过程中。

Mock 最大的功能是帮你把单元测试的耦合分解开,如果你的代码对另一个类或者接口有依赖,它能够帮你模拟这些依赖,并帮你验证所调用的依赖的行为。

三、Mock测试所需依赖

 1、主要引入mockito-core/powermock-core

        <dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>4.5.1</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-core</artifactId><version>2.0.9</version></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>2.0.9</version></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><version>2.0.9</version><scope>test</scope></dependency>

 Mockito和PowerMock都是单元测试模拟框架,用于模拟被测试类的依赖项。Mockito基于动态代理的方式实现,而PowerMock在Mockito基础上增加了类加载器以及字节码篡改技术,使其可以实现对private/static/final方法的Mock 

四、Mock的核心功能

1、Mock对象创建

Mockito.mock(List.class); // Mock对象创建

public class VerifyMockExample {@Testpublic void testVerifyMock() {List<String> mockList = Mockito.mock(List.class);// 调用Mock对象的方法mockList.add("testCode");mockList.size();// 验证mockList.add("test")是否被调用过一次Mockito.verify(mockList,Mockito.times(1)).add("testCode");// 验证size()方法是否被调用过Mockito.verify(mockList,Mockito.times(1)).size();Assert.assertFalse(mockList.size()==1);}
}

verify系列方法 

Mockito.verify/Mockito.times()验证调用次数 

·verify(mock).methodCall():验证方法被调用

· verify(mock, times(n)).methodCall():验证方法被调用n次

· verify(mock, never()).methodCall():验证方法从未被调用

或者是通过注解来实现创建

    @Mockprivate UserInfoMapper mockUserInfoMapper;@InjectMocksprivate UserInfoServiceImpl userInfoServiceImplUnderTest;

    @Testpublic void testVerifyMock2(){List<String> mockList = Mockito.mock(List.class);mockList.add("Code1");mockList.add("Code2");// 验证是否调用两次Mockito.verify(mockList,Mockito.times(2));}
 // 验证这个方法从没有被调用过Mockito.verify(userMapper, Mockito.never()).getUserById(1);userMapper.getUserById(1);// 验证这个方法至少调用了1次Mockito.verify(userMapper, Mockito.atLeastOnce()).getUserById(1);userMapper.getUserById(1);// 验证当前方法调用了2次Mockito.verify(userMapper, Mockito.times(2)).getUserById(1);

 Mockito.when()使用when和thenReturn方法配置Mock对象的行为

Mockito.when( 对象.方法名 ).thenReturn( 自定义结果) //当调用了某个 Mock 对象的方法时,就回传我们想要的自定义结果。 

thenReturn系列方法 

//当使用任何整数值调用 userService 的 getUserById 方法时,就回传一个名字为 I'm mock 3 的 User 对象。
Mockito.when(userService.getUserById(Mockito.anyInt)).thenReturn( newUser( 3, "I'm mock"));
//限制只有当参数的数字是 3 时,才会回传名字为 I'm mock 3 的 user 对象。
Mockito.when(userService.getUserById( 3)).thenReturn( newUser( 3, "I'm mock"));
//当调用 userService 的 insertUser 方法时,不管传进来的 user 是什么,都回传 100。
Mockito.when(userService.insertUser(Mockito.any(User.class))).thenReturn( 100);

thenThrow系列方法 

//当调用 userService 的 getUserById 时的参数是 8 时,抛出一个 RuntimeException。
Mockito.when(userService.getUserById( 8)).thenThrow( new RuntimeException( "mock throw exception"));
//如果方法没有返回值的话(即方法定义为 public void myMethod {...}),要改用 doThrow 抛出 Exception。
Mockito.doThrow( new RuntimeException( "mock throw exception")).when(userService.print);

 

    @Testpublic void testVerifyMock3(){List<String> mockList = Mockito.mock(List.class);// 设置Mock对象的预期行为Mockito.when(mockList.get(0)).thenReturn("mockedValue");// 断言验证返回值Assert.assertEquals("mockedValue",mockList.get(0));}

实战案例:测试一个UserInfoServiceImpl层saveUser()方法.

原始方法:

    @Transactional@Overridepublic void saveUser(UserInfo userInfo){userInfoMapper.saveUser(userInfo);}

单元测试:

    @Testpublic void testSaveUser() {// Setupfinal UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("userName");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());// Run the testuserInfoServiceImplUnderTest.saveUser(userInfo);// Verify the resultsMockito.verify(mockUserInfoMapper).saveUser(new UserInfo());}

原始方法:

    @Overridepublic List<UserInfo> queryListByUserName(String userName) {return userInfoMapper.queryListByUserName(userName);}

 单元测试方法:

    @Testpublic void testQueryListByUserName() {// Setupfinal UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("userName");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> expectedResult = Arrays.asList(userInfo);// Configure UserInfoMapper.queryListByUserName(...).final UserInfo userInfo1 = new UserInfo();userInfo1.setId(0);userInfo1.setUserName("userName");userInfo1.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo1.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> userInfos = Arrays.asList(userInfo1);when(mockUserInfoMapper.queryListByUserName("userName")).thenReturn(userInfos);// Run the testfinal List<UserInfo> result = userInfoServiceImplUnderTest.queryListByUserName("userName");// Verify the resultsassertThat(result).isEqualTo(expectedResult);}@Testpublic void testQueryListByUserName_UserInfoMapperReturnsNoItems() {// Setupwhen(mockUserInfoMapper.queryListByUserName("userName")).thenReturn(Collections.emptyList());// Run the testfinal List<UserInfo> result = userInfoServiceImplUnderTest.queryListByUserName("userName");// Verify the resultsassertThat(result).isEqualTo(Collections.emptyList());}

 原始方法:

    @Overridepublic List<UserInfo> queryUserInfoList(String createTime, List<Integer> idList) {return userInfoMapper.queryListByIds(createTime,idList);}

 单元测试方法:

    @Testpublic void testQueryUserInfoList() {// Setupfinal UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("userName");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> expectedResult = Arrays.asList(userInfo);// Configure UserInfoMapper.queryListByIds(...).final UserInfo userInfo1 = new UserInfo();userInfo1.setId(0);userInfo1.setUserName("userName");userInfo1.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo1.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> userInfos = Arrays.asList(userInfo1);when(mockUserInfoMapper.queryListByIds("createTime", Arrays.asList(0))).thenReturn(userInfos);// Run the testfinal List<UserInfo> result = userInfoServiceImplUnderTest.queryUserInfoList("createTime", Arrays.asList(0));// Verify the resultsassertThat(result).isEqualTo(expectedResult);}@Testpublic void testQueryUserInfoList_UserInfoMapperReturnsNoItems() {// Setupwhen(mockUserInfoMapper.queryListByIds("createTime", Arrays.asList(0))).thenReturn(Collections.emptyList());// Run the testfinal List<UserInfo> result = userInfoServiceImplUnderTest.queryUserInfoList("createTime", Arrays.asList(0));// Verify the resultsassertThat(result).isEqualTo(Collections.emptyList());}

原始方法:Controller层,MockMvc

mockMvc.perform(request):执行一个HTTP请求,并返回ResultActions对象。
ResultActions.andExpect(expected):验证请求的处理结果,如状态码、响应体等。
ResultActions.andDo(handler):处理请求的响应,如将响应体写入文件等。
ResultActions.andReturn():返回已执行请求的结果,以便直接访问结果。
MockMvc.perform(request).andExpect(expected).andDo(handler).andReturn():链式调用,执行请求、验证结果并处理响应,返回结果。


@Slf4j
@RestController
public class UserInfoController {@Autowiredprivate UserInfoService userInfoService;/*** 查询全部列表* @return*/@RequestMapping("/boot/query/users")public List<UserInfo> getUserInfoList(){List<UserInfo> list = userInfoService.list();return list;}/*** 保存用户信息* @return*/@RequestMapping("/boot/save/user")public String saveUser(){try {UserInfo user=new UserInfo();user.setUserName("MyBatis Log Free");userInfoService.save(user);} catch (Exception e) {log.error("save user error", e);throw new GlobalException("save user error");}return "success";}@RequestMapping("/boot/query/users/ids")public List<UserInfo> getUserInfoListIds(){List<Integer> list = Arrays.asList(1, 3, 5);String createTime="2022-09-05 15:11:21";List<UserInfo> userInfos = userInfoService.queryUserInfoList(createTime,list);return userInfos;}@RequestMapping("/boot/query/users/like")public List<UserInfo> getUserInfoListLike(){List<UserInfo> infoList = userInfoService.queryListByUserName("A");return infoList;}@RequestMapping("/boot/save/userinfo")public void saveUserInfo(){UserInfo userInfo = new UserInfo();userInfo.setId(5);userInfo.setUserName("Puck");Date startTime = new Date();Date endTime = new Date();userInfo.setCreateTime(startTime);userInfo.setUpdateTime(endTime);userInfoService.saveUser(userInfo);}
}

 单元测试方法如下:

@RunWith(SpringRunner.class)
@WebMvcTest(UserInfoController.class)
public class UserInfoControllerTest {@Autowiredprivate MockMvc mockMvc;@MockBeanprivate UserInfoService mockUserInfoService;@Testpublic void testGetUserInfoList() throws Exception {// Setup// Configure UserInfoService.list(...).final UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("MyBatis Log Free");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> userInfos = Arrays.asList(userInfo);when(mockUserInfoService.list()).thenReturn(userInfos);// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());
//        assertThat(response.getContentAsString()).isEqualTo("expectedResponse");}@Testpublic void testGetUserInfoList_UserInfoServiceReturnsNoItems() throws Exception {// Setupwhen(mockUserInfoService.list()).thenReturn(Collections.emptyList());// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("[]");}@Testpublic void testSaveUser() throws Exception {// Setupwhen(mockUserInfoService.save(new UserInfo())).thenReturn(false);// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/save/user").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("expectedResponse");verify(mockUserInfoService).save(new UserInfo());}@Testpublic void testGetUserInfoListIds() throws Exception {// Setup// Configure UserInfoService.queryUserInfoList(...).final UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("MyBatis Log Free");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> userInfos = Arrays.asList(userInfo);when(mockUserInfoService.queryUserInfoList("2022-09-05 15:11:21", Arrays.asList(0))).thenReturn(userInfos);// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users/ids").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("expectedResponse");}@Testpublic void testGetUserInfoListIds_UserInfoServiceReturnsNoItems() throws Exception {// Setupwhen(mockUserInfoService.queryUserInfoList("2022-09-05 15:11:21", Arrays.asList(0))).thenReturn(Collections.emptyList());// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users/ids").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("[]");}@Testpublic void testGetUserInfoListLike() throws Exception {// Setup// Configure UserInfoService.queryListByUserName(...).final UserInfo userInfo = new UserInfo();userInfo.setId(0);userInfo.setUserName("MyBatis Log Free");userInfo.setCreateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());userInfo.setUpdateTime(new GregorianCalendar(2020, Calendar.JANUARY, 1).getTime());final List<UserInfo> userInfos = Arrays.asList(userInfo);when(mockUserInfoService.queryListByUserName("A")).thenReturn(userInfos);// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users/like").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("expectedResponse");}@Testpublic void testGetUserInfoListLike_UserInfoServiceReturnsNoItems() throws Exception {// Setupwhen(mockUserInfoService.queryListByUserName("A")).thenReturn(Collections.emptyList());// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/query/users/like").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("[]");}@Testpublic void testSaveUserInfo() throws Exception {// Setup// Run the testfinal MockHttpServletResponse response = mockMvc.perform(get("/boot/save/userinfo").accept(MediaType.APPLICATION_JSON)).andReturn().getResponse();// Verify the resultsassertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value());assertThat(response.getContentAsString()).isEqualTo("expectedResponse");verify(mockUserInfoService).saveUser(new UserInfo());}
}

 @Mock用于模拟不属于 Spring 上下文的对象,而@MockBean用于模拟属于 Spring Boot 应用程序中的 Spring 上下文的对象。@MockBean提供与 Spring Boot 测试框架的无缝集成,并允许在测试期间用模拟对象轻松替换实际 bean 

Mockito提供了多种参数匹配器(Matchers)用于更灵活的验证和配置行为:

import static org.mockito.ArgumentMatchers.*;
when(mockRepository.findById(anyInt())).thenReturn(Optional.of(user));
verify(mockRepository).findById(eq(1));

常见的匹配器包括:

  ·any():匹配任何参数

  · anyInt():匹配任何整数参数

  · eq(value):匹配特定值

  · isNull():匹配null值

  · notNull():匹配非null值

Mock异常

Mockito.when(userMapper.getUserById(Mockito.anyInt())).thenThrow(new RuntimeException("运行时错误"));Assertions.assertThrowsExactly(RuntimeException.class, () -> userMapper.getUserById(1));// 对于没有返回值的方法,不能使用thenThrow()来抛出异常,可以使用doThrow()来抛出异常。
Mockito.doThrow(new RuntimeException("运行时错误")).when(userMapper).getUserById(1);
Assertions.assertThrowsExactly(RuntimeException.class, () -> userMapper.getUserById(1));

@MockBean 是 Spring Boot 提供的注解,它用于在测试中创建一个 mock 对象,并将其注入到 Spring 上下文中,替换掉原来的真实 Bean。 需要使用SpringJUnit4ClassRunner.class之类的注解

@Mock: 用于代替Mockito.mock创建mock对象,创建一个Mock实例,需要基于JUnit5环境。@InjectMocks: 创建一个实例,其余用@Mock(或@Spy)注解创建的mock将被注入到用该实例中。

你要测试哪个类(如TemplateUserServiceImpl ),那么就用 @InjectMocks注解;被测试的类中通过 @Autowired注解注入了几个,那么测试类里面就用@Mock注解创建几个实例!

原始代码:

@Service
public class TemplateUserServiceImpl implements TemplateUserService {@Autowiredprivate NamedParameterJdbcTemplate jdbcTemplate;@Transactional@Overridepublic void saveUser() {UserInfo userInfo = new UserInfo();userInfo.setId(7);userInfo.setUserName("Boot");Date startTime = new Date();Date endTime = new Date();userInfo.setCreateTime(startTime);userInfo.setUpdateTime(endTime);// JdbcTemplate的写入datetime,使用in方式
//        String sql="insert into user_info(user_name,create_time,update_time) values(:user_name,:create_time,:update_time)";String sql="insert into user_info(user_name,create_time,update_time) values(:userName,:createTime,:updateTime)";SqlParameterSource sqlParameterSource=new BeanPropertySqlParameterSource(userInfo);
//        HashMap paramMap = new HashMap<>();
//        paramMap.put("user_name",userInfo.getUserName());
//        paramMap.put("create_time",userInfo.getCreateTime());
//        paramMap.put("update_time",userInfo.getUpdateTime());
//        jdbcTemplate.update(sql,paramMap);jdbcTemplate.update(sql,sqlParameterSource);}
}

 对应单元测试代码:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;@RunWith(MockitoJUnitRunner.class)
public class TemplateUserServiceImplTest {@Mockprivate NamedParameterJdbcTemplate mockJdbcTemplate;@InjectMocksprivate TemplateUserServiceImpl templateUserServiceImplUnderTest;@Testpublic void testSaveUser() {// Setup// Run the testtemplateUserServiceImplUnderTest.saveUser();// Verify the resultsverify(mockJdbcTemplate).update(eq("insert into user_info(user_name,create_time,update_time) values(:userName,:createTime,:updateTime)"),any(SqlParameterSource.class));}@Testpublic void testSaveUser_NamedParameterJdbcTemplateThrowsDataAccessException() {// Setupwhen(mockJdbcTemplate.update(eq("insert into user_info(user_name,create_time,update_time) values(:userName,:createTime,:updateTime)"),any(SqlParameterSource.class))).thenThrow(DataAccessException.class);// Run the testassertThatThrownBy(() -> templateUserServiceImplUnderTest.saveUser()).isInstanceOf(DataAccessException.class);}
}

@MockBean 和 @SpyBean和@Spy以及@Mock的使用场景和区别 

 

五、单元测试生成插件

1、IDEA中安装Squaretest插件使用

文件右键即可生成对应单元测试,需要修改测试用例,满足业务诉求。

 

2、破解插件过程 

idea版本:

下载字节码编译工具:jclasslib。

jclasslib下载地址 

JAR包路径:C:\Users\Administrator\AppData\Roaming\JetBrains\IntelliJIdea2023.3\plugins\Squaretest 

 说明:不用版本的Squaretest插件的jar包名称或许不一样,找空间最大的那个,约15M左右的那个。

最后一步点击保存按钮,选择“overwrite”时,此时一定要将idea关闭,否则会保存失败的. 

最后破解成功,可以正常使用。

 

相关文章:

Mockito+PowerMock+Junit单元测试

一、单元测试用途 1、日常开发团队要求规范&#xff0c;需要对开发需求代码进行单元测试并要求行覆盖率达到要求&#xff0c;DevOps流水线也会开设相关门禁阀值阻断代码提交&#xff0c;一般新增代码行覆盖率80%左右。 二、Mock测试介绍 1、Mock是为了解决不同的单元之间由于…...

Ncat: bind to :::7777: Address already in use报错问题解决

问题描述 Ncat: bind to :::7777: Address already in use. QUITTING. 具体解决方法 If you are in linux environment try, Use netstat -tulpn to display the processeskill -9 <pid> This will terminate the process If you are using windows, Use netstat -…...

Docker 搭建mysql 连接超时问题,xxl-job启动mysql连接报错,禁用dns

1.本地连接Navicat报错信息&#xff0c;猜测是navicat默认连接超时导致的&#xff0c;后面换成idea一个插件虽然慢但连接上了 2013 - Lost connection to MySQL server at reading initial communication packet 2.启动xxl-job会报错&#xff0c;网上有人mysql驱动与数据库不匹…...

在线图片像素颜色拾取工具

在线图片像素颜色拾取工具&#xff0c;非常方便的一个工具&#xff0c;无需登录&#xff0c;用完就走。 包括中文和英文版本。 https://getcolor.openai2025.com...

Qt之登录界面(splash)

在上一篇多文档窗口设计(MDI)的基础上增加了一个登录界面&#xff08;splash&#xff09;. 该模块可以扩展为常规的软件登录界面。 界面展示如下 如果用户名和密码输入正确&#xff0c;则调到MDI界面&#xff0c;如果用户名和密码一共输入三次以上&#xff0c;则程序强制退出…...

NotebookLM:Google 最新 AI 笔记助理解析与实战应用

NotebookLM&#xff1a;Google 最新 AI 笔记助理解析与实战应用 在 AI 驱动的生产力工具不断进化的今天&#xff0c;Google 推出的 NotebookLM&#xff08;Notebook Language Model&#xff09;成为了一款备受关注的智能笔记助理。它结合了 Google 的大语言模型&#xff08;LL…...

软路由系统iStoreOS 一键安装 docker compose

一键安装命令 大家好&#xff01;今天我来分享一个快速安装 docker-compose 的方法。以下是我常用的命令&#xff0c;当前版本是 V2.32.4。如果你需要最新版本&#xff0c;可以查看获取docker compose最新版本号 部分&#xff0c;获取最新版本号后替换命令中的版本号即可。 w…...

vue3本地文件下载

开发记录&#xff1a; vue3本地下载文件要把文件放到public下&#xff0c;如果放在src里面可能会出现这个问题...

纯代码实现给WordPress添加文章复制功能

在给wordpress添加内容时&#xff0c;有时会遇到文章复制的功能&#xff0c;但是wordpress又没有这个功能。把下面一段代码添加到functions.php文件中&#xff0c;就可以实现这个功能。 /** Function for post duplication. Dups appear as drafts. User is redirected to the…...

Redis 中 TTL 的基本知识与禁用缓存键的实现策略(Java)

目录 前言1. 基本知识2. Java代码 前言 &#x1f91f; 找工作&#xff0c;来万码优才&#xff1a;&#x1f449; #小程序://万码优才/r6rqmzDaXpYkJZF 单纯学习Redis可以看我前言的Java基本知识路线&#xff01;&#xff01; 对于Java的基本知识推荐阅读&#xff1a; java框架…...

【PyQt】图像处理系统

[toc]pyqt实现图像处理系统 图像处理系统 1.创建阴影去除ui文件 2.阴影去除代码 1.创建阴影去除ui文件 UI文件效果图&#xff1a; 1.1QT Desiger设置组件 1.两个Pushbutton按钮 2.两个label来显示图像 3.Text Browser来显示输出信息 1.2布局的设置 1.先不使用任何La…...

Ruby语言的循环实现

Ruby语言的循环实现深入探讨 在程序设计中&#xff0c;循环是一种常见的控制结构&#xff0c;用于重复执行某些代码块。不同的编程语言提供了不同类型的循环结构&#xff0c;以满足不同的需求。Ruby是一种灵活且易于使用的编程语言&#xff0c;其循环实现方式独具一格&#xf…...

javaEE安全开发 SQL预编译 Filter过滤器 Listener 监听器 访问控制

前言 java开发和其他开发的不同并且更安全就是因为他拥有简单的预编译机制 filter 过滤器 和 listener 监听器 这个很重要 就是 web应用监听器和过滤器是在 Servlet 之前的并且 我们的请求和响应都需要经过 两者的同意才可以通过 缺一不可 、 Listener 安全方面 监听器…...

一体机cell服务器更换内存步骤

一体机cell服务器更换内存步骤&#xff1a; #1、确认grdidisk状态 cellcli -e list griddisk attribute name,asmmodestatus,asmdeactivationoutcome #2、offline griddisk cellcli -e alter griddisk all inactive #3、确认全部offline后进行关机操作 shutdown -h now #4、开…...

Hadoop•用Web UI查看Hadoop状态词频统计

听说这里是目录哦 通过Web UI查看Hadoop运行状态&#x1f407;一、关闭防火墙二、在物理计算机添加集群的IP映射三、启动集群四、进入HDFS的Web UI 词频统计&#x1f9a9;1、准备文本数据2、在HDFS创建目录3、上传文件4、查看文件是否上传成功5、运行MapReduce程序6、查看MapRe…...

rhel7.9利用有网络环境打包ansible

RHEL7.9激活(可省略) # 注册 subscription-manager register --usernameyour_username --passwordyour_password --auto-attach # 查看订阅状态 subscription-manager list # 将 “enabled1” 改为 “enabled0” vi /etc/yum/pluginconf.d/subscription-manager.conf 配置阿…...

vim文本编辑器三种模式的转换关系

输入模式 ———— 末行模式 输入模式和末行模式不能相互转换。 输入模式 ———— 命令模式 输入模式可以通过点击esc进入命令模式。 命令模式可以通过点击i进入输入模式。 末行模式 ———— 命令模式 末行模式可以通过点击esc进入命令模式。 命令模式可以通过shift&…...

深度学习:大模型Decoding+MindSpore NLP分布式推理详解

大模型推理流程 1. 用户输入提示词&#xff08;Prompt&#xff09; 假设用户输入为&#xff1a;“从前&#xff0c;有一只小猫&#xff0c;它喜欢……” 我们的目标是让模型生成一段完整的故事。 2. 模型处理用户输入 2.1 分词&#xff1a;输入提示被分词为模型可以理解的…...

【JVM中的三色标记法是什么?】

JVM中的三色标记法是什么? 一、基本概念二、标记过程三、优势与问题四、漏标与多标的解决方案三色标记法(Tri-color Marking Algorithm)是Java虚拟机(JVM)中一种用于追踪对象存活状态的垃圾回收算法。 它基于William D. Hana和Mark S. McCulleghan在1976年提出的两色标记法…...

数据库服务体系结构

1. 数据库服务应用配置 服务进行配置有什么作用&#xff1f; 实现服务运行启动 实现某些功能 应用配置有三种方式&#xff1f; 利用编译安装进行配置 编写配置文件信息 ,.默认的配置文件: /etc/my.cnf 利用启动命令参数配置信息&#xff0c;mysqld_safe --skip-grant-tables --…...

XML Group端口详解

在XML数据映射过程中&#xff0c;经常需要对数据进行分组聚合操作。例如&#xff0c;当处理包含多个物料明细的XML文件时&#xff0c;可能需要将相同物料号的明细归为一组&#xff0c;或对相同物料号的数量进行求和计算。传统实现方式通常需要编写脚本代码&#xff0c;增加了开…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

Python:操作 Excel 折叠

💖亲爱的技术爱好者们,热烈欢迎来到 Kant2048 的博客!我是 Thomas Kant,很开心能在CSDN上与你们相遇~💖 本博客的精华专栏: 【自动化测试】 【测试经验】 【人工智能】 【Python】 Python 操作 Excel 系列 读取单元格数据按行写入设置行高和列宽自动调整行高和列宽水平…...

【Java学习笔记】Arrays类

Arrays 类 1. 导入包&#xff1a;import java.util.Arrays 2. 常用方法一览表 方法描述Arrays.toString()返回数组的字符串形式Arrays.sort()排序&#xff08;自然排序和定制排序&#xff09;Arrays.binarySearch()通过二分搜索法进行查找&#xff08;前提&#xff1a;数组是…...

vscode(仍待补充)

写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh&#xff1f; debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路

进入2025年以来&#xff0c;尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断&#xff0c;但全球市场热度依然高涨&#xff0c;入局者持续增加。 以国内市场为例&#xff0c;天眼查专业版数据显示&#xff0c;截至5月底&#xff0c;我国现存在业、存续状态的机器人相关企…...

用机器学习破解新能源领域的“弃风”难题

音乐发烧友深有体会&#xff0c;玩音乐的本质就是玩电网。火电声音偏暖&#xff0c;水电偏冷&#xff0c;风电偏空旷。至于太阳能发的电&#xff0c;则略显朦胧和单薄。 不知你是否有感觉&#xff0c;近两年家里的音响声音越来越冷&#xff0c;听起来越来越单薄&#xff1f; —…...

短视频矩阵系统文案创作功能开发实践,定制化开发

在短视频行业迅猛发展的当下&#xff0c;企业和个人创作者为了扩大影响力、提升传播效果&#xff0c;纷纷采用短视频矩阵运营策略&#xff0c;同时管理多个平台、多个账号的内容发布。然而&#xff0c;频繁的文案创作需求让运营者疲于应对&#xff0c;如何高效产出高质量文案成…...

nnUNet V2修改网络——暴力替换网络为UNet++

更换前,要用nnUNet V2跑通所用数据集,证明nnUNet V2、数据集、运行环境等没有问题 阅读nnU-Net V2 的 U-Net结构,初步了解要修改的网络,知己知彼,修改起来才能游刃有余。 U-Net存在两个局限,一是网络的最佳深度因应用场景而异,这取决于任务的难度和可用于训练的标注数…...

Linux 下 DMA 内存映射浅析

序 系统 I/O 设备驱动程序通常调用其特定子系统的接口为 DMA 分配内存&#xff0c;但最终会调到 DMA 子系统的dma_alloc_coherent()/dma_alloc_attrs() 等接口。 关于 dma_alloc_coherent 接口详细的代码讲解、调用流程&#xff0c;可以参考这篇文章&#xff0c;我觉得写的非常…...