[英]Mocking multiple resttemplate in a single test method using mockito
我需要为一个方法编写测试用例,该方法有多个不同响应类型的resttemplate.getForObject()
调用。 所以我这样写:
@Test
public void tempMethodTest(){
doReturn(abc).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any());//1st rest call
doReturn(def).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any());//2nd rest call
doReturn(efg).when(restTemplate).getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any());//3rd rest call
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<ABC>>any())).thenReturn(abc);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<DEF>>any())).thenReturn(def);
//when(restTemplate.getForObject(anyString(), ArgumentMatchers.<Class<EFG>>any())).thenReturn(efg);
assertNotNull(service.tempMethod(obj));
}
但是在测试时,我在第一次 REST 调用期间遇到了提供的异常:
com.example.EFG cannot be cast to com.example.ABC
所以最后我找到了解决这个问题的方法。
@Test
public void getBaseStationConfigurationTest(){
when(restTemplate.getForObject(anyString(), any())).thenReturn(abc,def,efg);
assertNotNull(service.tempMethod(obj));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.