带有RestTemplate模拟的SpringBoot UnfinishedStubbingException

时间:2018-11-27 04:37:04

标签: java spring-boot junit mockito resttemplate

我正在尝试模拟RestTemplate,但是在调试时我看到了下面的错误,它会产生一个空指针:

  

方法引发了“ org.mockito.exceptions.misusing.UnfinishedStubbingException”异常。无法评估org.springframework.http.ResponseEntity $$ EnhancerByMockitoWithCGLIB $$ 3c63496d.toString()

enter image description here

这是我要测试的代码行:

 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();

0 个答案:

没有答案