idea 安装插件TestMe, 进入具体的类,选中类名 右键
1.多线程测试用例
@Mock Executor taskExecutor;
或者
@Mock ThreadPoolExecutor threadPoolExecutor;
都可用如下方法:
doAnswer((InvocationOnMock invocation) -> {
((Runnable) invocation.getArguments()[0]).run();
return null;
}).when(taskExecutor).execute(any(Runnable.class));
@Mock ScheduledThreadPoolExecutor executor;
executor.schedule(()->{
........
},10, TimeUnit.MINUTES);
doAnswer(invocation -> {
((Runnable) invocation.getArguments()[0]).run();
return null;
}).when(executor).schedule(any(Runnable.class),anyLong(), any());
@InjectMocks Test test;
@BeforeEach
void setUp() {
test = new test(a);//避免抢占资源,每次创建
}
@AfterEach
void after() {
test = null;//每次销毁
}
2.ReflectionTestUtils.setField() 用法:
@InjectMocks
TestService testService; //当前的测试类
1).这种注入方式
@Value("${test.active.switch:0}")
private String activeSwitch;
测试用例写法:
ReflectionTestUtils.

本文介绍了在Java单元测试中的一些实用技巧,包括使用TestMe插件,多线程测试,ReflectionTestUtils的使用,存储过程测试,第三方接口测试,静态方法测试,Redis和MongoDB的测试案例,以及如何处理多次调用返回不同结果的情况。详细展示了Mockito和相关工具的用法,以确保测试的全面性和准确性。
最低0.47元/天 解锁文章
1160

被折叠的 条评论
为什么被折叠?



