RESTful Web Services Integration Testing with Spring Boot

Share this:
  • 1
  •  
  •  
  •  
  •  
  •  
    1
    Share

This article shows an example of Integration Testing of RESTful Web Services using Spring Boot’s test framework support. It uses @SpringBootTest annotation to load an EmbeddedWebApplicationContext and provide a real servlet environment. The in-memory database is used to perform database operations. 

This example shows integration tests of RESTful Web Services of CRUD operations explained in the earlier article.

Technology Stack

Technology stack used in this example is:

  • Spring Boot 1.4.1.RELEASE
  • Spring Data JPA
  • Database – PostgreSQL, HSQLDB
  • JDK 8

 

Integration Testing

Before starting, something about integration testing. As the name suggests, in integration testing we have to test integration of all layers of the application. The environment of integration testing should be as similar as possible to the actual application environment where it supposed to run. Unlike unit testing, all application layers should communicate with each other. For example, data should be sent from Web layer to DAO layer(through Service layer) and hit the actual database. Most integration tests are written for the top layer like Web, and expects a proper response after processing through Service and DAO layer.

The application under test is a RESTful Web Service for CRUD operations using Spring Boot and Spring Data JPA. It uses PostgreSQL as a database. The application is planned to deploy on tomcat server.

To perform integration tests for this application we will create the following environment:

  • Deploy the application on Embedded Tomcat Server on random port
  • Use HSQLDB in-memory database
  • Initialize the database with some data for testing
  • Use TestRestTemplate from Spring Boot to call the Restful Web Services
  • No mocking or stubbing of any operation

Now take a look at project configuration to achieve all above things.

Project Setup

You just need two additional dependencies in our RESTFul Web Service project’s pom.xml  to start writing integration test cases.

  1. spring-boot-starter-test  : Spring Boot Test Framework with libraries including JUnit, Mockito etc.
  2. Library for HSQLDB database (org.hsqldb::hsqldb)

 

Apart from pom.xml changes we also need two additional files:

init-data.sql

This SQL file contains SQL commands to initialize the data required for test cases. We added two SQL statements. First to truncate the employee table and second to insert one record of the employee. “delete” and “get” operations will use this record in the test case.

test.properties

This is property file for testing. Here specify the database connection details for testing. Most of the cases it is an in-memory database like HSQLDB, H2 etc. If you want you can create a separate test instance of the database used by an application.  You can also specify different logging levels for testing. In our example, debug level is set for RestTemplate class to check the sent requests.

 

Project structure will be as follows:

integration test Spring Boot

Integration Test Class Configuration

Let’s start writing the Test class responsible for integration testing of our RESTful Web Service. First, take a look at class level configuration:

 

Take a look at each configuration one by one:

  • @RunWith(SpringRunner.class): It is an alias for SpringJUnit4ClassRunner . It will add Spring TestContext Framework support.
  • @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT): This will create applicationContext for testing. The weEnvironment property configured to WebEnvironment.RANDOM_PORT loads an EmbeddedWebApplicationContext and provides real servlet environment. It starts an embedded servlet container at random(free) port. This also registers the TestRestTemplate bean to make a rest call.
  • @TestPropertySource(locations = "classpath:test.properties"): This loads the application properties for testing from the specified location. Here we configure the HSQLDB connection properties. You may create a separate instance of PostgreSQL database(or any other) for testing. You may also put some additional log configurations for testing. Like setting DEBUG logging level for RestTemplate in our example.
  • @Sql({ "classpath:init-data.sql" }): Used to load the initial data for test database from SQL file in the classpath. Since this is specified at the class level, it will execute before each test case.
  • restTemplate: This is an autowired instance of TestRestTemplate registered by @SpringBootTest.

Writing Integration Test Cases

Integration testing will follow same steps as unit testing explained here. Those steps are Preparation, Execution, and Verification. The only difference is we no longer require to mock any layer’s behavior. So preparation step only requires for Add and Update operation to create the object to Add/Update. Now, let’s start with first Integration test.

Integration Test for Get operation

The method under test:

 

Below are the key steps this method is performing:

  • To get the Employee it makes one call to Service layer which is empService.getById(id)
  • It returns ResponseEntity with Employee object with HTTPStatus OK / NOT_FOUND based on service response.

We need to verify these things in our integration test. So an integration test should:

  • Sent GET request with “id” parameter set to employee id to find.
  • Check the HTTPStatus of response.
  • Check the returned object.

To send the GET request we use getForEntity() method of TestRestTemplate already autowired in test class. ResponseEntity’s getStatusCodeValue() and getBody() will provide the status and returned object.

We can have two test cases here. First with a positive scenario when an employee is found and another negative scenario when the employee is not found. And here is the code snippet of these two test cases.

Test Cases:

 

Note: Check the source code for the test case of “getAll” operation.

All other test cases will follow above way of testing. Make a rest call using TestRestTemplate and verify the ResponseEntity object. Below are the code snippets for rest of the methods.

Integration Test for Update operation

The method under test:

Test Case:

A thing to note in above test case is exchange method of TestRestTemplate is used to make rest call with PUT HttpMethod. This is because our method under test returns a ResponseEntity with HttpStatus. If your Update method is void, you can use put(URI url, Object request) method of TestRestTemplate.

Integration Test for Delete operation

The method under test:

Test Case:

 

Similar to update,  exchange method is used to make rest call because our method under test returns ResponseEntity. You may have a void method in Controller to perform the delete operation. In that case, you can use delete(URI url) method of TestRestTemplate.

Integration Test for Add operation

The method under test:

Test Case:

 

Hope you have got all basics of writing the integration test for Restful web services using Spring Boot.

Source Code

The complete source code for Integration testing of all CRUD operations in RESTful Web Services is on GitHub

 


Share this:
  • 1
  •  
  •  
  •  
  •  
  •  
    1
    Share