16
16
17
17
package sample .test .service ;
18
18
19
- import org .junit .Before ;
20
19
import org .junit .Rule ;
21
20
import org .junit .Test ;
22
21
import org .junit .rules .ExpectedException ;
22
+ import org .junit .runner .RunWith ;
23
23
import sample .test .domain .VehicleIdentificationNumber ;
24
24
25
+ import org .springframework .beans .factory .annotation .Autowired ;
26
+ import org .springframework .boot .test .autoconfigure .web .client .RestClientTest ;
25
27
import org .springframework .core .io .ClassPathResource ;
26
28
import org .springframework .http .HttpStatus ;
27
29
import org .springframework .http .MediaType ;
30
+ import org .springframework .test .context .junit4 .SpringRunner ;
28
31
import org .springframework .test .web .client .MockRestServiceServer ;
29
32
import org .springframework .web .client .HttpServerErrorException ;
30
33
39
42
*
40
43
* @author Phillip Webb
41
44
*/
45
+ @ RunWith (SpringRunner .class )
46
+ @ RestClientTest ({ RemoteVehicleDetailsService .class , ServiceProperties .class })
42
47
public class RemoteVehicleDetailsServiceTests {
43
48
44
49
private static final String VIN = "00000000000000000" ;
45
50
46
51
@ Rule
47
52
public ExpectedException thrown = ExpectedException .none ();
48
53
54
+ @ Autowired
49
55
private RemoteVehicleDetailsService service ;
50
56
57
+ @ Autowired
51
58
private MockRestServiceServer server ;
52
59
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
-
61
60
@ Test
62
61
public void getVehicleDetailsWhenVinIsNullShouldThrowException () throws Exception {
63
62
this .thrown .expect (IllegalArgumentException .class );
@@ -68,7 +67,7 @@ public void getVehicleDetailsWhenVinIsNullShouldThrowException() throws Exceptio
68
67
@ Test
69
68
public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails ()
70
69
throws Exception {
71
- this .server .expect (requestTo ("http://example.com /vehicle/" + VIN + "/details" ))
70
+ this .server .expect (requestTo ("/vehicle/" + VIN + "/details" ))
72
71
.andRespond (withSuccess (getClassPathResource ("vehicledetails.json" ),
73
72
MediaType .APPLICATION_JSON ));
74
73
VehicleDetails details = this .service
@@ -80,7 +79,7 @@ public void getVehicleDetailsWhenResultIsSuccessShouldReturnDetails()
80
79
@ Test
81
80
public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException ()
82
81
throws Exception {
83
- this .server .expect (requestTo ("http://example.com /vehicle/" + VIN + "/details" ))
82
+ this .server .expect (requestTo ("/vehicle/" + VIN + "/details" ))
84
83
.andRespond (withStatus (HttpStatus .NOT_FOUND ));
85
84
this .thrown .expect (VehicleIdentificationNumberNotFoundException .class );
86
85
this .service .getVehicleDetails (new VehicleIdentificationNumber (VIN ));
@@ -89,7 +88,7 @@ public void getVehicleDetailsWhenResultIsNotFoundShouldThrowException()
89
88
@ Test
90
89
public void getVehicleDetailsWhenResultIServerErrorShouldThrowException ()
91
90
throws Exception {
92
- this .server .expect (requestTo ("http://example.com /vehicle/" + VIN + "/details" ))
91
+ this .server .expect (requestTo ("/vehicle/" + VIN + "/details" ))
93
92
.andRespond (withServerError ());
94
93
this .thrown .expect (HttpServerErrorException .class );
95
94
this .service .getVehicleDetails (new VehicleIdentificationNumber (VIN ));
0 commit comments