java – Mockito和Deffered Result
作者:互联网
我有一个基于spring的项目,我正在努力改进其中的代码覆盖率
我有以下代码块,它在defferedResult onCompletion方法上使用lambda
util.getResponse(userInfoDeferredResult, url, userName, password);
userInfoDeferredResult.onCompletion(() -> {
//the result can be a String or ErrorResponse
//if ErrorResponse we don't want to cause ClassCastException and we don't need to cache the result
if (userInfoDeferredResult.getResult() instanceof String){
String response = (String) userInfoDeferredResult.getResult();
cacheServices.addValueToCache(Service.USER_INFO_CACHE_NAME, corpId, response);
}
});
我想知道 – 是否可以使用mockito或powerMockito来模拟onCompletion lambda的内容?
解决方法:
将内容提取到新方法:
if(userInfoDeferredResult.getResult() instanceof String) {
String response = (String) userInfoDeferredResult.getResult();
cacheServices.addValueToCache(Service.USER_INFO_CACHE_NAME, corpId, response);
}
然后用那种方法测试方法?
标签:java,powermock,spring-test,powermockito 来源: https://codeday.me/bug/20190715/1464899.html