Integration Testing in Spring Boot


When we talk about integration testing for a spring boot application, it is all about running an application in ApplicationContext and run tests. Spring Framework does have a dedicated test module for integration testing. It is known as spring-test. If we are using spring-boot, then we need to use spring-boot-starter-test which will internally use spring-test and other dependent libraries. 
In this article, we are going to see how integration tests can be run for a Spring Boot application. 


Spring-Boot provides an@SpringBootTest annotation which provides spring-boot features over and above of the spring-test module. This annotation works by creating the ApplicationContext used in our tests through SpringApplication. It starts the embedded server, creates a web environment and then enables @Test methods to do integration testing.
By default, @SpringBootTest  does not start a server. We need to add attribute webEnvironment to further refine how your tests run. It has several options:
  • MOCK(Default): Loads a web ApplicationContext and provides a mock web environment
  • RANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. The embedded server is started and listen on a random port. This is the one should be used for the integration test
  • DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment.
  • NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment
In the Spring Test Framework, we used to have @ContextConfiguration annotation in order to specify which spring @Configuration to load. However, It is not required in spring-boot as it automatically searches for the primary configuration when not defined. 
If we want to customize the primary configuration, we can use a nested @TestConfiguration class in addition to the application’s primary configuration.

Application Setup 

Let's set up a simple REST API application and see how to write integration tests on it. We will be creating a Student API to create and retrieve student record and write the integration tests for the controller. 

Maven Dependency

We will be using spring-boot, spring-data, h2 in-memory DB and spring-boot test for this example:


The controller will expose createStudent() and retrieveStudent() method as REST API:


This will implement the logic to call the repository to create and retrieve the student record:


Create a spring data repository StudentRepository, which implements all the CRUD operations: 


As explained above, for integrating testing of a spring-boot application, we need to use @SpringBootTest. spring-boot also does provide other classes like TestRestTemplate to test the REST APIs. Like RestTemplate class, it also does have methods getForObject(), postForObject(), exchange(), etc.Lets implement @Test methods to test create and retrieve both:
In the above code, we have used WebEnvironment.RANDOM_PORT to spin up the application on random port temporarily. @LocalServerPort helps to read the current port and build the URI to hit by template class. We have used exchange() method as we want to get ResponseEntity as return type. 


With TestRestTemplate, we have tested all the layers from Controller to DB layer end to end. However, sometimes DB setup or third party services won't be ready and you still want to test all the application layers in your scope. That would require mocking all the external systems and services. @MockBean helps to enable the mocking of a certain layer. In this example, we will be mocking the StudentRepository:
We have used Mockito methods along with @MockBean to set the expected response.


There is one more approach in which server start is not required at all, but test only the layer below that. In this approach, Spring handles the incoming HTTP request and hands it off to the controller. That way, almost the full stack is used, and the code will be called exactly the same way as if it was processing a real HTTP request, but without the cost of starting the server. To do that we will use Spring’s MockMvc, and to inject that we will be using another annotation called  @AutoConfigureMockMvc. Let's implement it for both create and retrieve use case:


With Spring 5, Webflux has been introduced to provide support for reactive streams. In that scenario, we would need to use WebTestClient class to test the REST API:


In this article, we have seen how we can do integration testing using Spring Boot test framework with the support of several different annotations.
As always, you can find the source code related to this article on GitHub.

No comments: