栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JMockit 简单小结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JMockit 简单小结

1、引入
		
		
		
		
			org.jmockit
			jmockit
			1.30
			test
		
		
			junit
			junit
			4.11
			test
		
2、前期准备
public class AppVersionTest {
	@Tested
    AppVersionService appVersionService;
     @Injectable
    AppVersionEntityMapper appVersionEntityMapper;
    @Injectable
    private RedisService redisService;

	@BeforeAll
    public static void before(){
    }
    
	public void init(){
        ReflectionTestUtils.setField(appVersionService, "host", "http://wwww.costco.com.cn");
        ReflectionTestUtils.setField(appVersionService, "iosUrl", "http://wwww.apple.com.cn");
    }
}

@Tested: 需要被测试的类
@Injectable: 注入依赖
@BeforeAll: 在当前类的所有测试方法之前执行
init(): 为被测试的类中的特定属性注入值

public class AppVersionService {
	@Autowired
    private AppVersionEntityMapper appVersionEntityMapper;
    @Autowired
    private RedisService redisService;
    @Value("${image.host}")
    private String host;
    @Value("${app.ios}")
    private String iosUrl;
}
3、unit test
	@Test
    public void getLastAppVersionByDeviceTest() {
        init();
        new Expectations() {
            {
                redisService.hasKey(anyString);
                result = true;
                redisService.get(anyString, AppVersionDto.class);
                AppVersionDto dto = new AppVersionDto();
                dto.setDescription("测试用例desc");
                result = dto;
            }
        };
        AppVersionDto dto = appVersionService.getLastAppVersionByDevice("android", 3);
        Assertions.assertEquals(dto.getDescription(), "测试用例desc");

录制函数行为
可以录制多个函数调用,result对应最近的调用函数的结果赋值。

new Expectations(){
	{
	}
}

验证
验证redisService.get() 是否调用了1次。

		new Verifications() {
            {
                redisService.get(anyString, AppVersionDto.class);
                times = 1;
            }
        };
4、特殊情况

1、被测试类中,有Example

	// 被测试方法
	public AutoEntity getAutoRenewalOrderDetail(String tradeNo){
        Example example = new Example(AutoEntity.class);
        example.createCriteria().andEqualTo("tradeNo", tradeNo);
        return autoEntityMapper.selectOneByExample(example);
    }
		EntityHelper.initEntityNameMap(AutoEntity.class, new Config());
        new Expectations(){
            {
                mapper.selectOneByExample(any);
                result = req;
            }
        };

2、被测试类中调用了其他方法或静态方法

		// getToken方法在DmHubService类中
		new MockUp(DmHubService.class) {
			// 如果是静态方法,需将String 前的 static 关键字去掉
            @Mock
            String getToken(String sendBody) {
                return "token";
            }
        };

私有方法不能被 MockUp

3、多线程

	// 被测试方法
	public void asycCacheUnreadMsgCount(List list){
        ExecutorService executorService = ThreadPoolUtils.getSingleExecutorService("asycCacheUnreadMsgCount");
        try {
            executorService.execute(() -> {
                list.forEach(e -> messageService.cacheMemberUnreadMsgCount(e.getMemberId(), 1L));
            });
        }finally {
            executorService.shutdown();
        }
    }
		new MockUp(ThreadPoolUtils.class) {
            @Mock
            public ThreadPoolExecutor getSingleExecutorService(String poolName){
            	// MockUp 返回的线程
                return new ThreadPoolExecutor(10, 10, 0L,
                        TimeUnit.MILLISECONDS,  new linkedBlockingQueue(10000)
                        ,new ThreadFactoryBuilder().setNameFormat(poolName + "-pool-%d").build(), new ThreadPoolExecutor.CallerRunsPolicy());
            }
        };
        new MockUp() {
            @Mock
            public void execute(Runnable command) {
            	// 执行线程
                command.run();
            }
        };
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/684030.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号