Skip to content

Commit b843306

Browse files
committed
Update spring-boot-sample-test REST code
Update RemoteVehicleDetailsService and the related test to use the new RestTemplateBuilder class and @restclienttest annotation. See spring-projectsgh-6030 See spring-projectsgh-5507
1 parent 0a47594 commit b843306

File tree

3 files changed

+20
-25
lines changed

3 files changed

+20
-25
lines changed

spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/RemoteVehicleDetailsService.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.slf4j.LoggerFactory;
2121
import sample.test.domain.VehicleIdentificationNumber;
2222

23+
import org.springframework.boot.web.client.RestTemplateBuilder;
2324
import org.springframework.http.HttpStatus;
2425
import org.springframework.stereotype.Service;
2526
import org.springframework.util.Assert;
@@ -37,27 +38,22 @@ public class RemoteVehicleDetailsService implements VehicleDetailsService {
3738
private static final Logger logger = LoggerFactory
3839
.getLogger(RemoteVehicleDetailsService.class);
3940

40-
private final ServiceProperties properties;
41-
4241
private final RestTemplate restTemplate;
4342

44-
public RemoteVehicleDetailsService(ServiceProperties properties) {
45-
this.properties = properties;
46-
this.restTemplate = new RestTemplate();
47-
}
48-
49-
protected final RestTemplate getRestTemplate() {
50-
return this.restTemplate;
43+
public RemoteVehicleDetailsService(ServiceProperties properties,
44+
RestTemplateBuilder restTemplateBuilder) {
45+
this.restTemplate = restTemplateBuilder
46+
.rootUri(properties.getVehicleServiceRootUrl()).build();
5147
}
5248

5349
@Override
5450
public VehicleDetails getVehicleDetails(VehicleIdentificationNumber vin)
5551
throws VehicleIdentificationNumberNotFoundException {
5652
Assert.notNull(vin, "VIN must not be null");
57-
String url = this.properties.getVehicleServiceRootUrl() + "vehicle/{vin}/details";
58-
logger.debug("Retrieving vehicle data for: " + vin + " from: " + url);
53+
logger.debug("Retrieving vehicle data for: " + vin);
5954
try {
60-
return this.restTemplate.getForObject(url, VehicleDetails.class, vin);
55+
return this.restTemplate.getForObject("/vehicle/{vin}/details",
56+
VehicleDetails.class, vin);
6157
}
6258
catch (HttpStatusCodeException ex) {
6359
if (HttpStatus.NOT_FOUND.equals(ex.getStatusCode())) {

spring-boot-samples/spring-boot-sample-test/src/main/java/sample/test/service/ServiceProperties.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
@ConfigurationProperties
2929
public class ServiceProperties {
3030

31-
private String vehicleServiceRootUrl = "http://localhost:8080/vs/";
31+
private String vehicleServiceRootUrl = "http://localhost:8080/vs";
3232

3333
public String getVehicleServiceRootUrl() {
3434
return this.vehicleServiceRootUrl;

spring-boot-samples/spring-boot-sample-test/src/test/java/sample/test/service/RemoteVehicleDetailsServiceTests.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616

1717
package sample.test.service;
1818

19-
import org.junit.Before;
2019
import org.junit.Rule;
2120
import org.junit.Test;
2221
import org.junit.rules.ExpectedException;
22+
import org.junit.runner.RunWith;
2323
import sample.test.domain.VehicleIdentificationNumber;
2424

25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
2527
import org.springframework.core.io.ClassPathResource;
2628
import org.springframework.http.HttpStatus;
2729
import org.springframework.http.MediaType;
30+
import org.springframework.test.context.junit4.SpringRunner;
2831
import org.springframework.test.web.client.MockRestServiceServer;
2932
import org.springframework.web.client.HttpServerErrorException;
3033

@@ -39,25 +42,21 @@
3942
*
4043
* @author Phillip Webb
4144
*/
45+
@RunWith(SpringRunner.class)
46+
@RestClientTest({ RemoteVehicleDetailsService.class, ServiceProperties.class })
4247
public class RemoteVehicleDetailsServiceTests {
4348

4449
private static final String VIN = "00000000000000000";
4550

4651
@Rule
4752
public ExpectedException thrown = ExpectedException.none();
4853

54+
@Autowired
4955
private RemoteVehicleDetailsService service;
5056

57+
@Autowired
5158
private MockRestServiceServer server;
5259

53-
@Before
54-
public void setup() {
55-
ServiceProperties properties = new ServiceProperties();
56-
properties.setVehicleServiceRootUrl("http://example.com/");
57-
this.service = new RemoteVehicleDetailsService(properties);
58-
this.server = MockRestServiceServer.createServer(this.service.getRestTemplate());
59-
}
60-
6160
@Test
6261
public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exception {
6362
this.thrown.expect(IllegalArgumentException.class);
@@ -68,7 +67,7 @@ public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exceptio
6867
@Test
6968
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
7069
throws Exception {
71-
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
70+
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
7271
.andRespond(withSuccess(getClassPathResource("vehicledetails.json"),
7372
MediaType.APPLICATION_JSON));
7473
VehicleDetails details = this.service
@@ -80,7 +79,7 @@ public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
8079
@Test
8180
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
8281
throws Exception {
83-
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
82+
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
8483
.andRespond(withStatus(HttpStatus.NOT_FOUND));
8584
this.thrown.expect(VehicleIdentificationNumberNotFoundException.class);
8685
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));
@@ -89,7 +88,7 @@ public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
8988
@Test
9089
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException()
9190
throws Exception {
92-
this.server.expect(requestTo("http://example.com/vehicle/" + VIN + "/details"))
91+
this.server.expect(requestTo("/vehicle/" + VIN + "/details"))
9392
.andRespond(withServerError());
9493
this.thrown.expect(HttpServerErrorException.class);
9594
this.service.getVehicleDetails(new VehicleIdentificationNumber(VIN));

0 commit comments

Comments
 (0)