Commit b8433066 authored by Phillip Webb's avatar Phillip Webb

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
parent 0a475946
...@@ -20,6 +20,7 @@ import org.slf4j.Logger; ...@@ -20,6 +20,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import sample.test.domain.VehicleIdentificationNumber; import sample.test.domain.VehicleIdentificationNumber;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert; import org.springframework.util.Assert;
...@@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService { ...@@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService {
private static final Logger logger = LoggerFactory private static final Logger logger = LoggerFactory
.getLogger(RemoteVehicleDetailsService.class); .getLogger(RemoteVehicleDetailsService.class);
private final ServiceProperties properties;
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
public RemoteVehicleDetailsService(ServiceProperties properties) { public RemoteVehicleDetailsService(ServiceProperties properties,
this.properties = properties; RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = new RestTemplate(); this.restTemplate = restTemplateBuilder
} .rootUri(properties.getVehicleServiceRootUrl()).build();
protected final RestTemplate getRestTemplate() {
return this.restTemplate;
} }
@Override @Override
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin) public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
throws VehicleIdentificationNumberNotFoundException { throws VehicleIdentificationNumberNotFoundException {
Assert.notNull(vin, "VIN must not be null"); Assert.notNull(vin, "VIN must not be null");
String url = this.properties.getVehicleServiceRootUrl() + "vehicle/{vin}/details"; logger.debug("Retrieving vehicle data for: " + vin);
logger.debug("Retrieving vehicle data for: " + vin + " from: " + url);
try { try {
return this.restTemplate.getForObject(url, VehicleDetails.class, vin); return this.restTemplate.getForObject("/vehicle/{vin}/details",
VehicleDetails.class, vin);
} }
catch (HttpStatusCodeException ex) { catch (HttpStatusCodeException ex) {
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) { if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {
......
...@@ -28,7 +28,7 @@ import org.springframework.stereotype.Component; ...@@ -28,7 +28,7 @@ import org.springframework.stereotype.Component;
@ConfigurationProperties @ConfigurationProperties
public class ServiceProperties { public class ServiceProperties {
private String vehicleServiceRootUrl = "http://localhost:8080/vs/"; private String vehicleServiceRootUrl = "http://localhost:8080/vs";
public String getVehicleServiceRootUrl() { public String getVehicleServiceRootUrl() {
return this.vehicleServiceRootUrl; return this.vehicleServiceRootUrl;
......
...@@ -16,15 +16,18 @@ ...@@ -16,15 +16,18 @@
package sample.test.service; package sample.test.service;
import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import sample.test.domain.VehicleIdentificationNumber; 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.core.io.ClassPathResource;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer; import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.HttpServerErrorException;
...@@ -39,6 +42,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat ...@@ -39,6 +42,8 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat
* *
* @author Phillip Webb * @author Phillip Webb
*/ */
@RunWith(SpringRunner.class)
@RestClientTest({ RemoteVehicleDetailsService.class, ServiceProperties.class })
public class RemoteVehicleDetailsServiceTests { public class RemoteVehicleDetailsServiceTests {
private static final String VIN = "00000000000000000"; private static final String VIN = "00000000000000000";
...@@ -46,18 +51,12 @@ public class RemoteVehicleDetailsServiceTests { ...@@ -46,18 +51,12 @@ public class RemoteVehicleDetailsServiceTests {
@Rule @Rule
public ExpectedException thrown = ExpectedException.none(); public ExpectedException thrown = ExpectedException.none();
@Autowired
private RemoteVehicleDetailsService service; private RemoteVehicleDetailsService service;
@Autowired
private MockRestServiceServer server; 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 @Test
public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exception { public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exception {
this.thrown.expect(IllegalArgumentException.class); this.thrown.expect(IllegalArgumentException.class);
...@@ -68,7 +67,7 @@ public class RemoteVehicleDetailsServiceTests { ...@@ -68,7 +67,7 @@ public class RemoteVehicleDetailsServiceTests {
@Test @Test
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails() public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
throws Exception { throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"), .andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
MediaType.APPLICATION_JSON)); MediaType.APPLICATION_JSON));
VehicleDetails details = this.service VehicleDetails details = this.service
...@@ -80,7 +79,7 @@ public class RemoteVehicleDetailsServiceTests { ...@@ -80,7 +79,7 @@ public class RemoteVehicleDetailsServiceTests {
@Test @Test
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException() public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
throws Exception { throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withStatus(HttpStatus.NOT_FOUND)); .andRespond(withStatus(HttpStatus.NOT_FOUND));
this.thrown.expect(VehicleIdentificationNumberNotFoundException.class); this.thrown.expect(VehicleIdentificationNumberNotFoundException.class);
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN)); this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
...@@ -89,7 +88,7 @@ public class RemoteVehicleDetailsServiceTests { ...@@ -89,7 +88,7 @@ public class RemoteVehicleDetailsServiceTests {
@Test @Test
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException() public void getVehicleDetailsWhenResultIServerErrorShouldThrowException()
throws Exception { throws Exception {
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details")) this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
.andRespond(withServerError()); .andRespond(withServerError());
this.thrown.expect(HttpServerErrorException.class); this.thrown.expect(HttpServerErrorException.class);
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN)); this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment