Mockito 和延迟结果
Mockito and 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);
}
那这样测试方法?
如其他答案中所述,在这种情况下,将内容提取到新方法是很好的解决方案。
此外,您还可以在下面link找到有关的文章:
http://radar.oreilly.com/2014/12/unit-testing-java-8-lambda-expressions-and-streams.html
您的测试应该模拟 cacheServices,并执行 lambda。
通常我不喜欢更改测试代码的服务代码(例如提取到方法并使其成为 public 尽管它应该是私有的)。 onCompletion
方法在调用AsyncContext
的completed
时触发。所以可以通过以下方式进行测试:
@RunWith(SpringRunner.class)
@WebMvcTest(DeferredResultController.class)
public class DeferredResultControllerUnitTest {
@MockBean
CacheServices cacheServices;
@Autowired
private MockMvc mockMvc;
@Test
public void onCompletionTest() throws Exception {
mockMvc.perform(get("/your_url"))
.andDo(mvcResult -> mvcResult.getRequest().getAsyncContext().complete());
Mockito.verify(cacheServices)
.addValueToCache(Service.USER_INFO_CACHE_NAME, getExpextedCorpId(), getExpectedResponse());
}
}
与 @SpringBootTest
相比,@WebMvcTest
不会启动所有 Spring 应用程序上下文,因此它更轻量。
我有一个基于 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);
}
那这样测试方法?
如其他答案中所述,在这种情况下,将内容提取到新方法是很好的解决方案。
此外,您还可以在下面link找到有关的文章: http://radar.oreilly.com/2014/12/unit-testing-java-8-lambda-expressions-and-streams.html
您的测试应该模拟 cacheServices,并执行 lambda。
通常我不喜欢更改测试代码的服务代码(例如提取到方法并使其成为 public 尽管它应该是私有的)。 onCompletion
方法在调用AsyncContext
的completed
时触发。所以可以通过以下方式进行测试:
@RunWith(SpringRunner.class)
@WebMvcTest(DeferredResultController.class)
public class DeferredResultControllerUnitTest {
@MockBean
CacheServices cacheServices;
@Autowired
private MockMvc mockMvc;
@Test
public void onCompletionTest() throws Exception {
mockMvc.perform(get("/your_url"))
.andDo(mvcResult -> mvcResult.getRequest().getAsyncContext().complete());
Mockito.verify(cacheServices)
.addValueToCache(Service.USER_INFO_CACHE_NAME, getExpextedCorpId(), getExpectedResponse());
}
}
与 @SpringBootTest
相比,@WebMvcTest
不会启动所有 Spring 应用程序上下文,因此它更轻量。