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