我有一个休息模板,它在服务类中的方法中进行调用,如下所示:
public CustomerResponse someMethod() {
CustomerResponse response = restTemplate.exchange(url, HttpMethod.GET, null, CustomerRes.class).getBody();
return response;
}
当尝试在我的测试类中模拟 restTemplate 时,它会在调用模拟 restTemplate 的行上不断抛出 NullPointerException:
public void checkResponseIsNotNull() {
CustomerResponse customerResponseMock = mock(CustomerResponse.class);
when(restTemplate.exchange(url, HttpMethod.GET, null, CustomerResponse.class).getBody()).thenReturn(customerResponseMock);
CustomerResponse cr = service.someMethod();
Assert.assertNotNull(cr);
}
为什么会抛出 NullPointer?我之前嘲笑过一个 RestTemplate ,只是没有 getBody() 方法,这导致人们相信它是导致空指针的原因。
慕盖茨4494581
相关分类