我需要模拟以下方法调用carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location))
,但是当carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location))
调用时我应该只返回模拟tripdetails并跳过protoConverter.convertTrip(carBookRequest, location)
方法调用。
ProtoRequestAdapterTest.java
@RunWith(PowerMockRunner.class)
@PrepareForTest({ProtoRequestAdapter})
class ProtoRequestAdapterTest {
@Test
public void testPopulateCarBookTest() {
CarApiToProtoConverter carApiToProtoConverter;
carApiToProtoConverter = PowerMockito.mock(CarApiToProtoConverter.class);
PowerMockito.when(carApiToProtoConverter.convertTripDetails(carBookRequest, locale)).thenReturn(tripDetails);
}
}
ProtoRequestAdapter.java
class ProtoRequestAdapter {
private CarBookRequest populateCarBook(BookingRequest bookingRequest) {
CarBookRequest newCarBookReq = bookingRequest.getCarBookRequest();
CarBookRequest.Builder carBookBuilder = CarBookRequest.newBuilder();
ProtoConverter protoConverter =
new ProtoConverter(refData, location);
carBookBuilder.setTrip(protoConverter.convertTrip(carBookRequest, location));
return carBookBuilder;
}
}