The integration test for the preceding method is very simple. Take a look at the following test method:
@Test
fun `GET welcome-with-parameter returns "Hello World"`() {
// When
val body = restTemplate.getForObject(
"/welcome-with-parameter/name/Buddy",
WelcomeBean::class.java)
// Then
assertThat(body.message,
containsString("Hello World, Buddy"));
}
A few important things to note are as follows:
- restTemplate.getForObject("/welcome-with-parameter/name/Buddy", WelcomeBean::class.java): This matches against the variable template in the URI. We are passing in the name, Buddy.
- assertThat(response.getBody(), containsString("Hello World, Buddy")): We expect the response to contain the message with the name.
In this section, we looked at the basics of creating a simple REST service with Spring Boot. We also ensured that we have good unit tests and integration tests.