我正在尝试模拟RestTemplate,但是在调试时我看到了下面的错误,它会产生一个空指针:
方法引发了“ org.mockito.exceptions.misusing.UnfinishedStubbingException”异常。无法评估org.springframework.http.ResponseEntity $$ EnhancerByMockitoWithCGLIB $$ 3c63496d.toString()
这是我要测试的代码行:
ResponseEntity<EmailEntity[]> response =
restTemplate.exchange(apiURI, HttpMethod.GET, entity, EmailEntity[].class);
EmailEntity[] partialEmailEntity = response.getBody();
response.getBody()返回null。
这是我测试中的一段代码:
@RunWith(MockitoJUnitRunner.class)
public class EmailItemReaderTest {
@Mock
private RestTemplate restTemplate;
@InjectMocks
private EmailItemReader reader;
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
}
...
...
ResponseEntity<EmailEntity> mockEntity = Mockito.spy(new ResponseEntity(mockObject, HttpStatus.OK));
doReturn(mockEntity).when(restTemplate).exchange(
Mockito.any(URI.class),
Mockito.any(HttpMethod.class),
Mockito.any(HttpEntity.class),
EmailEntity[].class);
reader.read();