很抱歉,如果这个问题已经回答或太琐碎了,但是我是这个问题的新手,找不到答案。 为简化起见,以下是从Spring Boot Ref(https://github.com/philwebb/testing-spring-boot-applications)中获取的示例代码:
测试:
@Test
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() throws Exception {
this.server..expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
MediaType.APPLICATION_JSON)); // 1
VehicleDetails details = this.service
.getVehicleDetails(new VehicleIdentificationNumber(VIN)); // 2
assertThat(details.getMake()).isEqualTo("Honda"); // 3
}
服务:
@Override
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
throws VehicleIdentificationNumberNotFoundException {
Assert.notNull(vin, "VIN must not be null");
String url = this.properties.getRootUrl() + "vehicle/{vin}/details";
try {
return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
} catch (HttpStatusCodeException ex) {
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {
throw new VehicleIdentificationNumberNotFoundException(vin, ex);
}
throw ex;
}
}
我的问题是,如果我们删除第1行,那会发生什么?不应该给出相同的目的吗?第1行和第2行之间是什么关系?
感谢您的帮助。