@Mock注释后,模拟实例为null
我尝试运行此测试:
@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.
我想念什么?
回答:
当您要使用@Mock
注释时,应使用MockitoJUnitRunner
@RunWith(MockitoJUnitRunner.class)public class MockitoTest {
@Mock
private IRoutingObjHttpClient routingClientMock;
@Test
public void testSendRoutingRequest() throws Exception {
// ...
}
}
另请参阅本教程。
以上是 @Mock注释后,模拟实例为null 的全部内容, 来源链接: utcz.com/qa/419968.html