私は、いくつかのユニットテストに必要な依存関係を模擬するのにいくつかの困難を抱えています。私は、Mockitoの使用方法を正しく理解していないと思う。thenReturn()。ここで(methodX).thenReturn(resultY)の場合、methodXは呼び出されますか?
がある(うまくいけば、関連する)サービスのためのコードの一部は、私がテストしたい:
public class AppointmentServiceTest {
private UriInfo mockUriInfo;
private HttpResponse mockHttpResponse;
@Mock
private DataConnector mockDataConnector;
@InjectMocks
private AppointmentService appointmentService;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockUriInfo();
mockHttpResponse();
}
private void mockUriInfo() {
mockUriInfo = mock(UriInfo.class);
when(mockUriInfo.getRequestUri()).thenReturn(
URI.create("http://localhost:8080"));
}
private void mockHttpResponse() {
mockHttpResponse = mock(HttpResponse.class);
when(mockHttpResponse.getInputStream()).thenReturn(
IOUtils.toInputStream("{\"appointment\":{}}"));
}
@Test
public void testGetAppointment() throws Exception {
when(mockDataConnector.getAppointmentById(Mockito.anyString())).
thenReturn(mockHttpResponse);
when(appointmentService.validateResponse(Mockito.any(HttpResponse.class), Mockito.any(UriInfo.class))).
thenReturn(new Appointment());
appointmentService.getAppointment(mockUriInfo, "12345");
verify(mockDataConnector).getAppointment(Mockito.anyString());
}
}
:
@Singleton
public class AppointmentService {
private DataConnector connector;
@Inject
public AppointmentService(DataConnector connector) {
this.connector = connector;
}
public Appointment getAppointment(UriInfo uriInfo, String id) {
HttpResponse response = connector.getAppointment(id);
return validateResponse(response, uriInfo);
}
protected Appointment validateResponse(HttpResponse response, UriInfo uriInfo) {
try {
AppointmentWrapper appointmentWrapper = JacksonJsonUtility.readValue(response.getInputStream(), AppointmentWrapper.class);
if(appointmentWrapper.getAppointment()!=null) {
return appointmentConverterHelper(appointmentWrapper.getAppointment(), uriInfo.getPath());
}
} catch(IOException e) {
throw new ResponseParsingException("IOException: " + e.getMessage());
}
}
protected Appointment appointmentConverterHelper(ExternalAppointmentModel appointmentDto, String uriPath) {
if (appointmentDto == null) {
throw new BrickworkQueryException("Got an invalid appointment object from Brickwork!");
}
Customer customer = new Customer(new Name(appointmentDto.getCustomer().getName()),
appointmentDto.getCustomer().getEmail(),
String.valueOf(appointmentDto.getCustomer().getCode()));
return new Appointment(
appointmentDto.getId(),
customer);
}
}
私の理解では、この方法のための私のユニットテストは、次のようになりますということです
私のテストを実行すると、おそらく空である、appointmentDto.getCustomer()を呼び出そうとするときに、appointmentConverterHelperメソッドでNullPointerExceptionが発生します。コードが実行されているので、mockHttpResponseに完全に有効なAppointmentを含むInputStreamを返す必要があるようです。私はwhen(appointmentService.validateResponse(mockHttpResponse, mockUriInfo)).thenReturn(new Appointment());
を持っているので、validateResponse
コールを受け取り、実際にvalidateResponseを実行するのではなく、空のAppointmentオブジェクトを返してはいけませんか?
参照のため、ここでは、上記のwhen
ステートメントは、AppointmentServiceTestのException-line 140のスタックトレースです。私は問題がwhen
文は、特定のインスタンスに対して一致しようとしていると失敗していることかもしれないと思う
java.lang.NullPointerException
at com.nike.appointmentservice.service.AppointmentsService.validatedResponse(AppointmentsService.java:176)
at com.nike.appointmentservice.service.AppointmentsServiceTest.testGetAppointmentById(AppointmentsServiceTest.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)```
NPEのスタックトレースを送信できますか? –
@ Code-Apprenticeスタックトレースが追加されました – DanHam