1616
1717package sample .test .service ;
1818
19- import org .junit .Before ;
2019import org .junit .Rule ;
2120import org .junit .Test ;
2221import org .junit .rules .ExpectedException ;
22+ import org .junit .runner .RunWith ;
2323import sample .test .domain .VehicleIdentificationNumber ;
2424
25+ import org .springframework .beans .factory .annotation .Autowired ;
26+ import org .springframework .boot .test .autoconfigure .web .client .RestClientTest ;
2527import org .springframework .core .io .ClassPathResource ;
2628import org .springframework .http .HttpStatus ;
2729import org .springframework .http .MediaType ;
30+ import org .springframework .test .context .junit4 .SpringRunner ;
2831import org .springframework .test .web .client .MockRestServiceServer ;
2932import org .springframework .web .client .HttpServerErrorException ;
3033
3942 *
4043 * @author Phillip Webb
4144 */
45+ @ RunWith (SpringRunner .class )
46+ @ RestClientTest ({ RemoteVehicleDetailsService .class , ServiceProperties .class })
4247public 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