Integration testing of SpringBoot with MS SQL Server using TestContainers

Saurav Kumar
3 min readJun 30, 2023

--

What is integration testing?

Integration testing is a type of software testing in which individual software modules are combined and tested as a group. The goal of integration testing is to verify that the different modules of a software application can interact with each other correctly. Integration testing typically occurs after unit testing, which is the process of testing individual software modules in isolation.

Why is integration testing important?

Integration testing is important because it helps to ensure that the different components of a software application work together as expected. By testing the interactions between modules, integration testing can help to identify and fix potential problems early in the development process, before they cause major issues. Integration testing can also help to improve the quality of the software application by ensuring that it is more reliable and robust.

How can TestContainers be used for integration testing?

To use TestContainers for integration testing, developers simply need to add the library to their project and then use the provided APIs to create and manage instances of the dependencies they need to test against.

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mssqlserver</artifactId>
<version>1.18.3</version>
<scope>test</scope>
</dependency>

TestContainers is a Java library that provides a way to run integration tests against real-world dependencies, such as databases, web services, and messaging systems. This allows developers to test their applications in a more realistic environment and to identify and fix potential problems that would not be found with traditional unit testing.

Create MSSQLServerContainer

This test first creates an MS SQL Server container using the MSSQLServerContainer variable. The container is configured with the same database name, username, and password.

@Container
private static final MSSQLServerContainer<?> SQLSERVER_CONTAINER = new MSSQLServerContainer<>(
"mcr.microsoft.com/mssql/server:2022-latest").acceptLicense();

Add properties for the MSSQL Server

@DynamicPropertySource
static void setProperties(DynamicPropertyRegistry dynamicPropertyRegistry) {
dynamicPropertyRegistry.add("spring.datasource.url", SQLSERVER_CONTAINER::getJdbcUrl);
dynamicPropertyRegistry.add("spring.datasource.username", SQLSERVER_CONTAINER::getUsername);
dynamicPropertyRegistry.add("spring.datasource.password", SQLSERVER_CONTAINER::getPassword);
}

Start the MSSQLServerContainer

static {
SQLSERVER_CONTAINER.start();
}

Test cases

Once you have configured Testcontainers, you can start writing your unit tests. Here is an example of a unit test that tests the connection to the MSSQLServer database:

@Test
@Order(value = 1)
void testConnectionToDatabase() {
Assertions.assertNotNull(repository);
}

@Test
@Order(value = 2)
void testAddEmployees() throws Exception {
for (EmployeeRequest employee : employees) {
String emp = objectMapper.writeValueAsString(employee);
mockMvc.perform(
MockMvcRequestBuilders.post("/api/v1/employees").contentType(MediaType.APPLICATION_JSON).content(emp))
.andExpect(status().isCreated());
}
Assertions.assertEquals(5, repository.findAll().size());
}

@Test
@Order(value = 3)
void testGetAllEmployees() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/employees")).andExpect(status().isOk());
Assertions.assertEquals(employees.get(3).getName(), repository.findById(4).get().getName());
}

@Test
@Order(value = 4)
void testGetEmployeeById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/employees/2")).andExpect(status().isOk());
Assertions.assertEquals(employees.get(1).getName(), repository.findById(2).get().getName());
}

@Test
@Order(value = 5)
void testDeleteEmployeeById() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/api/v1/employees/2")).andExpect(status().isOk());
}

@Test
@Order(value = 6)
void testUpdateEmployee() throws Exception {
Employee employee = Employee.builder().id(3).name("Saurav Kumar Shah").address("India East").build();
String emp = objectMapper.writeValueAsString(employee);
mockMvc.perform(
MockMvcRequestBuilders.put("/api/v1/employees").contentType(MediaType.APPLICATION_JSON).content(emp))
.andExpect(status().isOk());
Assertions.assertEquals(employee.getName(), repository.findById(3).get().getName());
}

@Test
@Order(value = 7)
void testDeleteAllEmployees() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/api/v1/employees")).andExpect(status().isOk());
Assertions.assertEquals(0, repository.findAll().size());
}

What are the benefits of using TestContainers?

There are several benefits to using TestContainers for integration testing, including:

  • Realistic testing environment: TestContainers allows developers to test their applications against real-world dependencies, such as databases, web services, and messaging systems. This allows developers to identify and fix potential problems that would not be found with traditional unit testing.
  • Efficient testing: TestContainers can be used to run integration tests in parallel, which can significantly improve the efficiency of the testing process.
  • Easy to use: TestContainers is a simple and easy-to-use library. Developers can quickly and easily add it to their projects and start using it to run integration tests.
  • Wide range of supported dependencies: TestContainers supports a wide range of dependencies, including databases, web services, and messaging systems. This allows developers to test their applications against a variety of different environments.

Overall, TestContainers is a powerful and versatile library that can be used to improve the quality of software applications by providing a more realistic and efficient testing environment.

Source code reference

The source code for the examples in this blog can be found on GitHub:

https://github.com/sauravkumarshah/springboot-with-sqlserver

--

--

Saurav Kumar
Saurav Kumar

Written by Saurav Kumar

Experienced Software Engineer adept in Java, Spring Boot, Microservices, Kafka & Azure.

No responses yet