Skip to content

Instantly share code, notes, and snippets.

@salex89
Last active December 14, 2016 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salex89/57cc58ace24d0c128589e4f9bd2efc2b to your computer and use it in GitHub Desktop.
Save salex89/57cc58ace24d0c128589e4f9bd2efc2b to your computer and use it in GitHub Desktop.
Spring test 1.4 example
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT/*, classes = Application.class*/)
//@DataJpaTest //Starts with in-memory database, without a web tier, only data tier
//@RestClientTest({RemoteVehicleDetailsService.class}) //Use with REST client class
//@TestPropertySource(properties = "vehicle.service.root-url=http://example.com")
//@JsonTest //Testing JSON (de)serialization
//@AutoConfigureTestDatabase //self-explanatory
//@WebMvcTest(UserVehicleController.class) //MVC class
public class ApplicationIntegrationTest {
// @Value("${local.server.port}")
// private int port;
@LocalServerPort
private int port;
@Rule
public ExpectedException thrown = ExpectedException.none();
//Autowired TestRestTemplates can figure the local port out and use relative addresses
@Autowired
private TestRestTemplate restTemplate = new TestRestTemplate();
@Autowired
private MockMvc mvc; //Used for testing MVC applications. MockMvc does not boot up a complete server
@Autowired
private TestEntityManager testEntityManger; //alternative test-oriented entity manager
@Autowired
private MockRestServiceServer server; //REST client support, mock interactions with server
@Autowired
private JacksonTester<VehicleDetails> json;
@MockBean //Replaces current bean in context, from Mockito
private VehicleDetailsService VehicleDetailsService;
@Test
public void test() {
ResponseEntity<String> response = this.restTemplate
.getForEntity("/{username}/vehicle", String.class, "mickey");
assertThat(response.getBody(), containsString("Honda"));
}
@Test
public void testWithException() {
this.thrown.expect(IllegalArgumentException.class);
ResponseEntity<String> response = this.restTemplate
.getForEntity("/{username}/vehicle", String.class, "mickey");
assertThat(response.getBody(), containsString("Honda"));
}
@Test
public void testWithPersisting() {
this.testEntityManger.persist(new User("donald", "123456789"));
User user = this.repository.findByUsername("minnie");
assertThat(user).isNull();
}
@Test
public void testRestClient()
{
this.server.expect(requestTo("http://example.com/vehicle" + VIN + "/details"))
.andRespond(withSuccess(getClassPathResource("file.json")));
//execute call
}
@Test
public void serializeJson()
{
//Uses AssertJ
VehicleDetails details = new VehicleDetails("Honda", "Civic");
assertthat(this.json.write(details)).isEqualTo("vehicledetails.json"); //loads file from same package
assertthat(this.json.write(details)).hasJsonPathStringValue("@.make").isEqualTo("Honda"); //loads file from same package
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment