BairesDev
  1. Blog
  2. Software Development
  3. Java Integration Testing Explained With Examples
Software Development

Java Integration Testing Explained With Examples

Explore the world of Java Integration Testing with our comprehensive guide. Understand tools, process, and best practices, complemented with practical examples.

BairesDev Editorial Team

By BairesDev Editorial Team

BairesDev is an award-winning nearshore software outsourcing company. Our 4,000+ engineers and specialists are well-versed in 100s of technologies.

17 min read

Featured image

As software systems grow larger and more complex, with components and services interacting in intricate ways, integration testing has become indispensable. By validating that all components and modules work correctly when combined, Java integration testing provides confidence that the overall system will operate as intended.

With the rise of modular architectures, microservices, and automated deployment, verifying these complex interactions early through integration testing is now a core discipline. Robust integration testing identifies defects arising from component interactions that unit tests alone cannot detect. Leveraging a Java integration testing framework can help streamline the process, ensuring that all modules and components are thoroughly vetted.

In today’s world of continuous delivery and DevOps, where rapid iterations and frequent upgrades are the norm, dependable integration testing is mandatory for quality and reducing technical debt.

This article explores the tools and techniques for effective integration testing in Java. For managing multiple integration tests efficiently, it’s common to group them into a test suite. Each test suite often consists of multiple test classes, where each class can represent a specific feature or component being tested. So whether you are working for a top Java development services company or you are a student looking to brush up on your testing skills, we will cover it all.

What is Unit Testing?

Unit Testing is a software testing process where individual units of code, such as methods, classes and modules are tested to see if they work as intended. It is the first step in the software testing life cycle. Unit testing in Java is generally done with JUnit. The purpose of unit tests is to isolate and verify the correctness of individual units of your code. A failed unit test can give early indications of potential issues, but integration tests will further ensure the entire system’s cohesion and functionality.

Imagine the process of assembling a car. Before assembling components together, each component is tested rigorously. This is more efficient and less time consuming, in the long run. Now imagine if all the components were assembled together without proper testing and then, the car is not working. It will take a lot of time and effort to even figure out which part is faulty and more to actually fix it.

That’s why we need unit tests. It makes it easier to identify bugs early on and save development time.

What is Java Integration Testing?

Integration testing is an approach to software testing where different modules are coupled together and tested. The goal is to see if the modules work as intended when coupled together. It’s generally performed after unit testing and before system testing.

Integration testing is especially important for applications that consist of multiple layers and components that communicate with each other and with external systems or services.

Imagine you’re building a personal computer (PC) from scratch. The PC consists of various components such as the motherboard, processor, memory, storage devices, graphics card, and so on. You have tested all of the components previously. But when you integrate them into a system, they don’t work. The reason is not because the individual components have some defect but rather, they are not compatible with each other. Integration testing helps us identify those kinds of errors.

Differences Between Integration Testing and Unit Testing

Scope: Unit Testing aims to test the smallest testable units of code, whereas Integration Testing focuses on testing the interaction of multiple components of a system.

Complexity: Unit tests tend to be simpler and more focused since they deal with individual components in isolation. They can be written and executed relatively easily. Integration tests, on the other hand, are generally more complex due to the need to coordinate and verify the interactions between multiple components. They require a higher level of setup and configuration to simulate real-world scenarios accurately.

Order of Execution: Generally, unit testing is performed before integration testing. We first need to verify if individual units are functional. Only then can we integrate them into larger modules and test their relationships.

Different Approach to Integration Testing

There are different strategies for conducting integration tests. The most common of them are the Big Bang approach and the incremental (top-down and bottom-up) approach.

Big Bang

In this approach, most of the software modules are coupled together and tested. It is suitable for small systems with fewer modules. In some cases, the components of a system may be tightly coupled or highly interdependent, making incremental integration difficult or impractical.

Advantages of Big Bang

  1. Convenient for smaller systems.
  2. It’s less time consuming compared to the other approaches.

Disadvantages of Big Bang

  1. With this approach, however, it is difficult to locate the root cause of the defects found while testing.
  2. Since you are testing most of the modules at once, it is easy to miss out on some integrations.
  3. Harder to maintain as complexity of the project increases.
  4. You have to wait for the development of most of the modules.

Bottom Up Approach

Bottom-up approach focuses on developing and testing the lowest level independent modules of a system before integrating them into larger modules. Those modules are tested and then integrated into still larger modules until the entire system is integrated and tested.

In this approach, testing starts from the simplest components and then moves upward, adding and testing higher level components until the entire system is built and tested.

The biggest advantage of this type of testing is that you don’t have to wait for the development of all the modules. Instead you can write tests for the ones that are already built. The biggest disadvantage is that you don’t have much clarity over the behavior of your critical modules.

Advantages

  1. Bottom-up emphasizes coding and early testing, which can begin as soon as the first module has been specified.
  2. Since the testing process is started from the low-level module, there is a lot of clarity and it’s easy to write tests.
  3. It is not necessary to know about the details of the structural design.
  4. It’s easier to develop test conditions in general, as you are starting from the lowest level.

Disadvantages

  1. If the system consists of a greater number of sub-modules, this approach becomes time-consuming and complex.
  2. Developers don’t have a clear idea about the behavior of the critical modules.

Top Down Approach

Top-down Java integration testing is an approach to software testing where the testing process starts from the topmost most critical modules of the software system and gradually progresses towards the lower-level modules. It involves testing the integration and interaction between different components of the software system in a hierarchical manner.

In top-down integration testing, the higher-level modules are tested first, while the lower-level modules are replaced with stubs or simulated versions that provide the expected behavior of the lower-level modules. As the testing progresses, the lower-level modules are gradually incorporated and tested in conjunction with the higher-level modules.

Advantages

  1. Critical modules are tested first.
  2. Developers have a clear idea about the behavior of the critical functionality of the application.
  3. Easy to detect issues at the top level.
  4. It is easier to isolate interface and data transfer errors due to the downward incremental nature of the testing process.

Disadvantages

  1. It requires use of mocks, stubs and spies.
  2. You still have to wait for the development of the critical modules.

Mixed (Sandwich) Approach

The mixed or sandwich approach is the combination of the bottom up approach and top down approach. Generally, with this approach you have multiple layers, each of which is built using either the top down or the bottom up approach.

Steps Involved in Integration Testing

Choosing the Right Tools and Frameworks

Java is a popular high-level programming language. It has a vast ecosystem of frameworks and libraries for testing. Here are some of the most commonly used tools for integration testing:

  1. JUnit5: A widely-used testing framework for Java that can be used to write both unit tests and integration tests.
  2. TestNG: Another popular testing framework that provides features like parallel test execution and test configuration flexibility.
  3. Spring Boot Test: If you’re working with Spring Boot, this module provides extensive support for Java integration testing, including the @SpringBootTest annotation.
  4. Mockito: A powerful mocking framework that allows you to mock dependencies and focus on testing specific components in isolation.
  5. Testcontainers: A Java library that allows you to define and run Docker containers for your dependencies during testing.

Throughout this article we will be using JUnit5 as our primary testing framework. We will have one or two examples with the Spring Boot Test framework. These frameworks have a very intuitive syntax, so it’s easy to follow along even if you are not using JUnit5.

Setup test environment

Setting up a test environment is the first step for Java integration testing. Ideally you would want to setup a database, mock some dependencies and add test data.

Adding Test data

Before starting a testing, you can populate your database using the Junit5 @BeforeEach annotation.

@DataJpaTest
public class UserRepositoryIntegrationTest {

    @Autowired
    private UserRepository userRepository;

    @BeforeEach
    public void setUpTestData() {
        User user1 = new User("John Doe", "[email protected]");
        User user2 = new User("Jane Smith", "[email protected]");

        userRepository.save(user1);        userRepository.save(user2);
    }
}

Mocks and Stubs

Mocking and stubbing helps you isolate your dependencies or mimic a dependency that has not been implemented yet. In the example given below, we are creating a mock for the UserRepository class.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class UserServiceTest {

    @Test
    public void testCreateUser() {
        UserRepository userRepository = mock(UserRepository.class);
        UserService userService = new UserService(userRepository);
        User user = new User("John Doe", "[email protected]");

        when(userRepository.save(user)).thenReturn(true);
        boolean result = userService.createUser(user);

        verify(userRepository, times(1)).save(user);
        assertEquals(true, result);
    }
}

Setting up H2 Database

H2 is an in-memory database that is often used for testing as it is fast and lightweight. To configure H2 for integration tests, you can use the @DataJpaTest annotation provided by Spring Boot. This annotation sets up an in-memory database, and you can use your JPA repositories to interact with it.

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import static org.junit.jupiter.api.Assertions.assertEquals;

@DataJpaTest
public class UserRepositoryIntegrationTest {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSaveUser() {
        User user = new User("John", "[email protected]");
        userRepository.save(user);
        User savedUser = userRepository.findByEmail("[email protected]");
        assertEquals(user.getName(), savedUser.getName());
        assertEquals(user.getEmail(), savedUser.getEmail());
    }
}

One thing to note is that your application might not be using H2 as your primary database. In that case, you are not mimicking your production environment. If you want to use databases PostgreSQL or MongoDB in your test environment, you should use containers instead (discussed later).

Recording and Reporting

One of the simplest ways of recording your tests is through logging. Logs can help you quickly identify or replicate the root cause of any defects in your application.

Here’s a simple logging setup using Logback and SLF4J. Logback helps us customize the logged messages. We can provide details like date, time, thread, loglevel, trace and much more. To get started create a logback.xml file and add the configuration as shown below.

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %level - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

Here, %d{HH:mm:ss.SSS} prints the time (H for hours, m for minutes, s for seconds and S for milliseconds). %thread, %level, %msg and %n prints the thread name, log level, message and new-line respectively. The appender created above displays information in the standard output. But you can do things like store the logs in a file.

Now we can use the Logger facade from SLF4J in our java code.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExampleTest {
    private static final Logger logger
            = LoggerFactory.getLogger(ExampleTest.class);

    public static void main(String[] args) {
        logger.info("Hello from {}", ExampleTest.class.getSimpleName());
    }
}

If you run it, the output will be something like this.

14:45:01.260 [main] INFO - Hello from ExampleTest

Running Containerized Tests

Docker containers are one of the best ways to mimic your production environment, since in many cases, your application itself is running inside some remote containers. For these demonstrations, we will write a simple test and then run it inside a docker container.

// SampleController.java
@RestController
public class SampleController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello world";
    }
}

// SampleApplication.java
@SpringBootApplication
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

The above code creates a simple REST api that returns “Hello world”. We can use Spring Boot Test to test this api. Here, we are simply checking whether the api returns a response or not and the response body must contain “Hello world”.

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class SampleApplicationTests {
    
    @LocalServerPort
    private int port;
    
    @Autowired
    private TestRestTemplate restTemplate;
    
    @Test
    public void testHelloEndpoint() {
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:" + port + "/hello", String.class);
        assertEquals(HttpStatus.OK, response.getStatusCode());
        assertEquals("Hello, World!", response.getBody());
    }
}

Now, you can create a Dockerfile and add the following configuration. The Dockerfile contains instructions on how to build your application.

FROM openjdk:17-jre-slim
WORKDIR /app
COPY target/sample-application.jar .
CMD ["java", "-jar", "sample-application.jar"]

Run the following command to build and start your docker container.

docker build -t sample-application .
docker run -d -p 8080:8080 sample-application

You can use docker-compose to orchestrate multiple containers. One challenge with docker compose is when you run integration tests; you cannot change the configuration during runtime. Moreover, your containers will keep running even if your tests have failed. Instead, we will use a library called testcontainers to dynamically start and stop containers.

Testcontainers

Testcontainers is a Java library that simplifies the process of running isolated, disposable containers for integration testing. It provides lightweight, pre-configured containers for popular databases (e.g., PostgreSQL, MySQL, Oracle, MongoDB), message brokers (e.g., Kafka, RabbitMQ), web servers (e.g., Tomcat, Jetty), and more.

Using Testcontainers, you can define and manage containers within your Java integration tests, allowing you to test against real instances of dependencies without the need for external infrastructure. Testcontainers handles container lifecycle management, automatic provisioning, and integration with testing frameworks like JUnit or TestNG.

import org.junit.jupiter.api.Test;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.junit.Assert.*;

@Testcontainers
public class PostgreSQLIntegrationTest {

    @Container
    private static final PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:latest")
            .withDatabaseName("db_name")
            .withUsername("johndoe")
            .withPassword("random_password");

    @Test
    public void testPostgreSQLContainer() {
           assertTrue(postgresContainer.isRunning())
    }
}

Best Practices While Writing Integration Tests

Start Writing Both Unit and Integration Tests Early On

In the traditional waterfall approach, tasks are performed sequentially. Testing often comes into play towards the latter stages of the development cycle. Since your application is tested later, the chances of bugs going unnoticed and making it to production is pretty high.

In contrast, with the agile approach you start writing your tests early on. It ensures that every time you make a small change in your codebase, you will get immediate feedback on whether your changes have any effect on the existing codebase. If a unit test fails and you realize there’s an issue, you can address it immediately, before it becomes a big issue in the later stages. That’s the primary advantage of the agile approach, where writing tests early provides continuous feedback, making it harder to introduce bugs at any stage.

Prioritizing Tests

Integration tests can be slow and, in scenarios where they require significant time and resources, running them repeatedly becomes impractical. In such situations, prioritizing your tests can save a lot of valuable time. You can prioritize tests based on factors such as the level of risk associated with a failure, the complexity of the functionality being tested, and the potential impact on end users or the system as a whole.

In Junit5, using test classes, you can add tags to your tests and prioritize them. Here’s an example.

// SampleTests.java
public class SampleTests {
 @Test
 @Tag("HIGH")
 public void testCriticalFunctionality() {}

 @Test
 @Tag("LOW")
 public void testLessCriticalFunctionality() {}
}

// HighPriorityTestSuite.java
// only testing the high priority integrations
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
@IncludeTags("HIGH")
public class HighPriorityTestSuite {}

Mimic Production Environments As Closely As Possible

Create test environments that closely resemble the production environment as much as possible. This includes setting up similar configurations, databases, network conditions, and any external dependencies.

Design Test Cases for All Scenarios

Design test cases and decide on the right test method for all scenarios. Create test cases and test methods that cover various scenarios and edge cases to ensure maximum coverage. Test positive scenarios and conduct negative integration testing, to verify the system’s behavior in different situations.

Record and Report Your Tests

When an integration test fails, especially in large software projects, it can be time-consuming to identify the cause. After a failed integration test, it’s beneficial to have recorded test runs, so you can easily identify the root cause or reproduce the issue.

Conclusion

In this article, we have explored the concept, benefits, and various java integration testing frameworks used for integration testing in Java. We also covered how to use frameworks like JUnit5, Spring Boot Test, TestContainers and Logback to write effective integration tests.Integration testing is essential, hence integration testing plays a crucial role in ensuring the performance, quality and functionality of our Java applications. It also allows us to verify the interactions and dependencies between different components and layers. We encourage you to explore more on the topic discussed here.

FAQs

How can I ensure test data consistency during integration testing?

For integration testing, it’s essential to set up and manage consistent test data for the various components. You can achieve this by using test data factories, test databases, or data seeding mechanisms.

What are the common challenges in Java integration testing?

Challenges in Java integration testing may include handling external dependencies like databases, services, or APIs, managing test environment configurations, and ensuring tests run efficiently and in a repeatable manner.

What is a “contract test” in the context of integration testing?

Contract tests are a form of integration testing that verifies the compatibility and communication between different services or components based on their defined contracts (e.g., API specifications or message formats)

How can I ensure the code quality before running integration tests?

Before executing integration tests, it’s beneficial to perform static code analysis. This process involves examining the software’s code without actually running it, aiming to detect vulnerabilities, potential bugs, and areas of improvement. Static code analysis ensures that the code meets quality standards, follows best practices, and is free from common errors, setting a solid foundation for subsequent testing phases.

Which Java build tools can assist in setting up integration testing environments?

Maven and Gradle are Java build tools that help set up integration test environments. Their plugins and configurations manage dependencies and execute test suites to standardize testing across teams.

If you enjoyed this, be sure to check out one of our other Java articles:

Tags:
BairesDev Editorial Team

By BairesDev Editorial Team

Founded in 2009, BairesDev is the leading nearshore technology solutions company, with 4,000+ professionals in more than 50 countries, representing the top 1% of tech talent. The company's goal is to create lasting value throughout the entire digital transformation journey.

Stay up to dateBusiness, technology, and innovation insights.Written by experts. Delivered weekly.

Related articles

Software Development - The Power of
Software Development

By BairesDev Editorial Team

18 min read

Contact BairesDev
By continuing to use this site, you agree to our cookie policy and privacy policy.