How to automate integration testing?

Wed, Feb 10, 2021

Read in 2 minutes

Introduction :

I use postman to test the services in integration environment manually .Every time when we push the code to integration environment, we need to sanity check all the services manually. Assume ,if we have more than 15 services and for each and every deployment you are manually testing ,then it’s a time consuming process. So We decided to automate the integration testing .

The Spring boot Service to Test: Here I am using a simple spring boot service for testing. The Git hub repo for the spring boot service is https://github.com/ramyakandasamy/CosumeRestAPI

This spring boot service will have controller ,service layer as usual and this service will consume the REST API https://jsonplaceholder.typicode.com/todos/ and expose the consumed data as response.

The endpoint for this service is http://localhost:8080/getDetails

How To automate Integration Testing?

ConsumeRestapiIntegTests.java

package com.example.demo;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest()

public class ConsumeRestapiIntegTests {
TestRestTemplate restTemplateobj = new TestRestTemplate();

@Test
public void testgetDetails() throws Exception {
List todoList =new ArrayList();
todoList = this.restTemplateobj.getForObject("http://localhost:8080/getDetails",List.class);
//here you can specify your integ env url
Assertions.assertNotNull(todoList);
Assertions.assertFalse(todoList.isEmpty());
Assertions.assertNotNull(todoList.get(0));
}

}

In the Test method , ,using restTemplateobj ,we call the service url and using juint assertion ,we can check the response object is not null and we can check other values too. In the test method you can specify the integration environment url to connect.

Conclusion :

For this blog ,I use spring boot project by adding springboot test module .

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>

If you want you can create simple maven project and you can add the service which you want to test as dependency.

If you have CI/CD pipeline ,you can configure this integration test project as a gateway to start the new deployment .

Unit testing will help in maintaining the code and Integration tests will help in regression testing.