Integration testing

When we integration testing, we will want to launch the embedded server with all the controllers and beans that are configured. The following block of code shows how we can create a simple integration test:

    @RunWith(SpringRunner::class)    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)    class BasicControllerIT {      @Autowired      lateinit var restTemplate: TestRestTemplate      @Test      fun `GET welcome returns "Hello World"`() {        // When        val body = restTemplate.getForObject("/welcome",         String::class.java)        // Then        assertThat(body).isEqualTo("Hello World")      }    }

A few important things to note are as follows:

  • @RunWith(SpringRunner::class), @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) ...

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.