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
pull/6081/head
Phillip Webb 9 years ago
parent 0a475946a1
commit b84330663d

@ -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())) {

@ -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;

@ -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));

Loading…
Cancel
Save