我尝试运行这个测试后空:模拟实例@Mock注释
@Mock IRoutingObjHttpClient routingClientMock;
@Mock IRoutingResponseRepository routingResponseRepositoryMock;
@Test
public void testSendRoutingRequest() throws Exception {
CompleteRoutingResponse completeRoutingResponse = new CompleteRoutingResponse();
completeRoutingResponse.regression_latencyMillis = 500L;
Mockito.when(routingClientMock.sendRoutingRequest(any(RoutingRequest.class))).thenReturn(completeRoutingResponse);
RoutingObjHttpClientWithReRun routingObjHttpClientWithReRun = new RoutingObjHttpClientWithReRun
(routingClientMock, routingResponseRepositoryMock);
...
}
,但我得到NullPointerException异常为:
Mockito.when(routingClientMock.
我失去了什么?
你叫'MockitoAnnotations.initMocks(this)'? (应该可能在@Before方法)或者,你有没有其他@Rule你期望初始化你的模拟? (它不是自动的) –
你需要实例化'routingClientMock'例如'routingClientMock = Mockito.mock(RoutingObjHtttpClient.class);' –
你也可以使用'@RunWith(MockitoJUnitRunner。class)'在你的课 –