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) ...