[英]Mocking Spring restTemplate.exchange call with parameterized reference type argument
我试图模拟对RestTemplate.exchange()的调用,但不能让它工作。 目前对exchange()的调用挂起,所以我相信实际的方法是被调用而不是我的模拟。 对exchange()的调用如下:
ResponseEntity<List<MyType>> response =
restTemplate.exchange(queryStr,
HttpMethod.GET,
null,
new ParameterizedTypeReference<List<MyType>>() {
});
嘲笑如下:
@MockBean
private RestTemplate restTemplate;
@Test
public void testMethod() throws Exception {
when(restTemplate.exchange(anyString(),
eq(HttpMethod.GET),
eq(null),
eq(new ParameterizedTypeReference<List<MyType>>(){})
)).thenReturn(new ResponseEntity<List<MyType>>(HttpStatus.OK));
// rest of test code follows.
}
我已经尝试更改参数匹配器,因此它们匹配更广泛的参数类型(即any()代替anyString())但我得到相同的行为或错误“对交换的引用是模糊的两种方法交换(... )和方法交换(...)匹配“。 我也得到“找不到合适的方法,然后返回(...)与thenReturn(...)不兼容”以及第一个错误。
提前致谢。
发现我们没有使用我们的控制器中使用的@Autowired注释RestTemplate的实例。
@RestController
public class myController {
...
@Autowired // <-- Forgot this annotation.
private RestTemplate restTemplate;
...
}
现在模拟工作正常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.