1. What is SpringBoot?
Spring Boot is an open-source micro framework, which provides Java developers with a platform to get started with an auto configurable production-grade Spring application.
- Comes with embedded HTTP servers like Tomcat or Jetty to test web applications.
- Adds many plugins that developers can use to work with embedded and in-memory databases easily. Spring allows you to connect easily with database and queue services like Oracle, PostgreSQL, MySQL, MongoDB, Redis, Solr, ElasticSearch, Rabbit MQ and others.
2. Integration Testing of Spring Boot Application
Integration testing of SpringBoot application is all about running an application in ApplicationContext and run tests. Spring Framework does have a dedicated test module for integration testing. It 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.
With the @SpringBootTest annotation, Spring Boot provides a convenient way to start up an application context to be use in a test. In this tutorial, we will discuss when to use @SpringBootTest and how to use it. SpringBoot provides the @SpringBootTest annotation, which we can use to create an application context containing all the objects we need for the Integration Testing It, starts the embedded server, creates a web environment and then enables methods to do Integration testing.
2.1 WebEnvironment
By default, @SpringBootTest does not start the webEnvironment to refine further how your tests run. It has several options:
1. MOCK(Default): Loads a web ApplicationContext and provides a mock web environment
2. RANDOM_PORT: Loads a WebServerApplicationContext and provides a real web environment. The embedded server is start and listen on a random port. This is the one should be used for the integration test
3. DEFINED_PORT: Loads a WebServerApplicationContext and provides a real web environment.
4. NONE: Loads an ApplicationContext by using SpringApplication but does not provide any web environment
2.2 Write integration tests with @SpringBootTest
Let’s add the Maven dependencies needed to test SpringBoot application
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
2.3 What is RestController?
SpringBoot RestController annotation is use to create RESTful web services using Spring MVC. Spring RestController takes care of mapping request data to the request defined handles method. This is the Spring boot rest controller used for the testing purpose
A convenience annotation that is itself annotated with @Controller
and @ResponseBody
Get() method returns Hello world if request sends “hello” word otherwise, response returns “Try saying ‘hello'”
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("helloworld")
public class TestController {
@GetMapping("/{greeting}")
public String get(@PathVariable String greeting) {
String response;
if(greeting.equals("hello")) {
response = "Hello World";
}else{
response = "Try saying 'hello'";
}
return response;
}
}
2.4 Integration Test
Let’s create a Feature file with a scenario where we send request to SpringBoot Application and get valid response.
Feature: Test SpringBoot Request
@ReceiveCorrectResponse
Scenario: Send a valid SpringBoot Request to get correct response
Given I send a springboot request to the URL "/helloworld/hello"
Then the springboot response will return "Hello World"
The test class mentioned below contains integration tests for the spring boot rest controller mentioned above. This test class:
- uses @SpringBootTest annotation which loads the actual application context.
- uses WebEnvironment.RANDOM_PORT to create run the application at some random server port.
- @LocalServerPort gets the reference of port where the server has started. It helps in building the actual request URIs to mimic real client interactions.
- io.restassured.RestAssured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SpringIntegrationTest {
private final static String BASE_URI = "http://localhost";
@LocalServerPort
private int port;
String endpoint = "/helloworld/hello";
private ValidatableResponse validatableResponse;
private void configureRestAssured() {
RestAssured.baseURI = BASE_URI;
RestAssured.port = port;
RestAssured.basePath = "/demo";
}
protected RequestSpecification getAnonymousRequest() throws NoSuchAlgorithmException
{
configureRestAssured();
return given();
}
@Given("^I send a springboot request to the URL \"([^\"]*)\"$")
public void iSendARequest(String endpoint) throws Throwable
{
validatableResponse = getAnonymousRequest().contentType(ContentType.JSON).when().get(endpoint).then();
}
@Then("^the springboot response will return \"([^\"]*)\"$")
public void extractResponse(String message)
{ validatableResponse.assertThat().statusCode(HttpStatus.OK.value()).body(containsString(message));
}
}
2.5 Demo
Run the above tests within Eclipse. The test class start the whole application in embedded server and the execute the test

The next tutorial is about the Integration Testing of SpringBoot Application using Serenity BDD and Cucumber
Can you please provide the imports for AbstractRestAssuredHelper class, or a github repository 🙂
LikeLike
I have updated the code. You should be able to see all the imports. The code is also in GitHub Repo. The code is updated with the latest dependencies.
LikeLike