Integration testing

The integration test for the preceding method is very simple. Take a look at the following test method:

    @Test
public void welcomeWithParameter() throws Exception {
ResponseEntity<String> response =
template.getForEntity(
createURL("/welcome-with-parameter/name/Buddy"), String.class);
assertThat(response.getBody(),
containsString("Hello World, Buddy"));
}

A few important things to note are as follows:

  • createURL("/welcome-with-parameter/name/Buddy"): 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. While these are really basic, they lay the foundation for more complex REST services we will build in the next section.

The unit tests and integration tests we implemented can have better asserts using a JSON comparison instead of a simple substring comparison. We will focus on it in the tests we write for the REST services we will create in the next sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset