Integration testing

When we do integration testing, we would want to launch the embedded server with all the controllers and beans that are configured. This code snippet shows how we can create a simple integration test:

    @RunWith(SpringRunner.class)    @SpringBootTest(classes = Application.class,     webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)    public class BasicControllerIT {      private static final String LOCAL_HOST =       "http://localhost:";      @LocalServerPort      private int port;      private TestRestTemplate template = new TestRestTemplate();      @Test      public void welcome() throws Exception {        ResponseEntity<String> response = template       .getForEntity(createURL("/welcome"), String.class);        assertThat(response.getBody(), equalTo("Hello World")); } ...

Get Mastering Spring 5.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.