【发布时间】:2021-01-19 14:22:29
【问题描述】:
我有以下Java 方法:
public Appointment addAppointment(String client, Appointment appointment) {
String esbUrl = new ESBUrlHelper().getEsbUrl();
AppointmentClient appointmentClient = AppointmentClientFactory.getUnsecuredClient(esbUrl);
if (appointment.getId() == null) {
outputAppointment = appointmentClient.addAppointment(client, appointment);
}
return outputAppointment;
}
上述方法调用第三方REST客户端appointmentClient。
我遇到的问题是这导致我的测试失败。
如何在单元测试中模拟 appointmentClientobject?
目前我的测试如下:
@Test
public void shouldAddAppointment() {
// act
Appointment appointment = appointmentService.addAppointment(CLIENT_STRING, appointmentMock)
// assert
assertNotNull(appointment);
}
但我在appointmentClient.addAppointment(client, appointment); 行收到以下错误:
org.jboss.resteasy.client.exception.ResteasyIOException: IOException
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
我想模拟如下:
Mockito.when(appointmentClient.addAppointment(client, appointment)).thenReturn(appointmentMock);
【问题讨论】:
标签: java rest junit mocking mockito