From b84330663da0110f4c44006c698e7cc78d2633ff Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 31 May 2016 09:29:24 -0700 Subject: [PATCH] Update spring-boot-sample-test REST code Update RemoteVehicleDetailsService and the related test to use the new RestTemplateBuilder class and @RestClientTest annotation. See gh-6030 See gh-5507 --- .../service/RemoteVehicleDetailsService.java | 20 +++++++--------- .../test/service/ServiceProperties.java | 2 +- .../RemoteVehicleDetailsServiceTests.java | 23 +++++++++---------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/RemoteVehicleDetailsService.java b/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/RemoteVehicleDetailsService.java index 0a99173501..606695eee1 100644 --- a/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/RemoteVehicleDetailsService.java +++ b/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/RemoteVehicleDetailsService.java @@ -20,6 +20,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import sample.test.domain.VehicleIdentificationNumber; +import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.util.Assert; @@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService { private static final Logger logger = LoggerFactory .getLogger(RemoteVehicleDetailsService.class); - private final ServiceProperties properties; - private final RestTemplate restTemplate; - public RemoteVehicleDetailsService(ServiceProperties properties) { - this.properties = properties; - this.restTemplate = new RestTemplate(); - } - - protected final RestTemplate getRestTemplate() { - return this.restTemplate; + public RemoteVehicleDetailsService(ServiceProperties properties, + RestTemplateBuilder restTemplateBuilder) { + this.restTemplate = restTemplateBuilder + .rootUri(properties.getVehicleServiceRootUrl()).build(); } @Override public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin) throws VehicleIdentificationNumberNotFoundException { Assert.notNull(vin, "VIN must not be null"); - String url = this.properties.getVehicleServiceRootUrl() + "vehicle/{vin}/details"; - logger.debug("Retrieving vehicle data for: " + vin + " from: " + url); + logger.debug("Retrieving vehicle data for: " + vin); try { - return this.restTemplate.getForObject(url, VehicleDetails.class, vin); + return this.restTemplate.getForObject("/vehicle/{vin}/details", + VehicleDetails.class, vin); } catch (HttpStatusCodeException ex) { if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) { diff --git a/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/ServiceProperties.java b/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/ServiceProperties.java index 9222fbd46f..e5a35de2fd 100644 --- a/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/ServiceProperties.java +++ b/spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/ServiceProperties.java @@ -28,7 +28,7 @@ import org.springframework.stereotype.Component; @ConfigurationProperties public class ServiceProperties { - private String vehicleServiceRootUrl = "http://localhost:8080/vs/"; + private String vehicleServiceRootUrl = "http://localhost:8080/vs"; public String getVehicleServiceRootUrl() { return this.vehicleServiceRootUrl; diff --git a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/service/RemoteVehicleDetailsServiceTests.java b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/service/RemoteVehicleDetailsServiceTests.java index 4216f56b00..40f600dfe9 100644 --- a/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/service/RemoteVehicleDetailsServiceTests.java +++ b/spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/service/RemoteVehicleDetailsServiceTests.java @@ -16,15 +16,18 @@ package sample.test.service; -import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import sample.test.domain.VehicleIdentificationNumber; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; import org.springframework.core.io.ClassPathResource; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.web.client.HttpServerErrorException; @@ -39,6 +42,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat * * @author Phillip Webb */ +@RunWith(SpringRunner.class) +@RestClientTest({ RemoteVehicleDetailsService.class, ServiceProperties.class }) public class RemoteVehicleDetailsServiceTests { private static final String VIN = "00000000000000000"; @@ -46,18 +51,12 @@ public class RemoteVehicleDetailsServiceTests { @Rule public ExpectedException thrown = ExpectedException.none(); + @Autowired private RemoteVehicleDetailsService service; + @Autowired private MockRestServiceServer server; - @Before - public void setup() { - ServiceProperties properties = new ServiceProperties(); - properties.setVehicleServiceRootUrl("http://example.com/"); - this.service = new RemoteVehicleDetailsService(properties); - this.server = MockRestServiceServer.createServer(this.service.getRestTemplate()); - } - @Test public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exception { this.thrown.expect(IllegalArgumentException.class); @@ -68,7 +67,7 @@ public class RemoteVehicleDetailsServiceTests { @Test public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() throws Exception { - this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) + this.server.expect(requestTo("/vehicle/" + VIN + "/details")) .andRespond(withSuccess(getClassPathResource("vehicledetails.json"), MediaType.APPLICATION_JSON)); VehicleDetails details = this.service @@ -80,7 +79,7 @@ public class RemoteVehicleDetailsServiceTests { @Test public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() throws Exception { - this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) + this.server.expect(requestTo("/vehicle/" + VIN + "/details")) .andRespond(withStatus(HttpStatus.NOT_FOUND)); this.thrown.expect(VehicleIdentificationNumberNotFoundException.class); this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN)); @@ -89,7 +88,7 @@ public class RemoteVehicleDetailsServiceTests { @Test public void getVehicleDetailsWhenResultIServerErrorShouldThrowException() throws Exception { - this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) + this.server.expect(requestTo("/vehicle/" + VIN + "/details")) .andRespond(withServerError()); this.thrown.expect(HttpServerErrorException.class); this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));