【发布时间】:2019-02-25 22:40:42
【问题描述】:
我正在尝试测试我的 spring rest 客户端,但我遇到了问题。这是我的客户端类:
@Component
public class MyClient{
private RestTemplate restTemplate;
@Autowired
public MyClient(RestTemplateBuilder restTemplateBuilder,ResponseErrorHandler myResponseErrorHandler) {
this.restTemplate = restTemplateBuilder
.errorHandler(myResponseErrorHandler)
.build();
}
//other codes here
}
这里的myResponseErrorHandler 是覆盖ResponseErrorHandler 类的handleError 和hasError 方法的类。
现在我的测试类是
@RunWith(SpringRunner.class)
public class MyClientTest {
@InjectMocks
MyClient myClient;
@Mock
RestTemplate restTemplate;
@Mock
RestTemplateBuilder restTemplateBuilder;
//test cases here
}
但我收到如下错误,我不确定如何解决此问题。
You haven't provided the instance at field declaration so I tried to construct the instance.
However the constructor or the initialization block threw an exception : null
【问题讨论】:
标签: junit mockito resttemplate