Introduction :#
Testing is a crucial part of software development, ensuring the quality and reliability of the system. It includes various test types, such as unit tests, integration tests, functional tests, and more, each focusing on different aspects of the system. Unit tests verify individual components, while integration tests ensure components work together as expected. Common testing annotations in Spring Boot, such as @SpringBootTest
and @DataJpaTest
, support efficient testing of different layers and components, contributing to a comprehensive testing strategy that ensures both functionality and performance.
TestContainer#
Use https://testcontainers.com/ to mock a real database, Use when mocking the repository or in Integration testing.
- Download the Docker Application from https://docs.docker.com/get-started/get-docker/ and run it.
- Alternatively, open Google and search for “docker download,” or visit one of the following links:
- https://www.docker.com/products/docker-desktop/
- Or https://docs.docker.com/desktop/install/windows-install/
- After downloading and installing Docker, open the application and run it.
- Add the following dependency:
- Go to https://start.spring.io/ configure your project settings, and then go to the 'Add Dependencies' section.
- Add the
Testcontainers
andPostgreSQL Driver
dependencies.
- After that, click on 'Explore.' Scroll down to confirm that these three dependencies have been added.
- pom.xmlfile :
Why We Use Testcontainer#
We use Testcontainers for several reasons, especially in integration testing:
- Realistic Testing Environment: Testcontainers allow us to run real databases (e.g., PostgreSQL, MySQL) or services (e.g., Redis, Kafka) in Docker containers. This provides a more realistic testing environment than in-memory databases like H2, ensuring that our tests reflect real-world scenarios.
- Isolated Test Environment: Each test runs in its own isolated container, ensuring that tests are consistent and do not interfere with each other. This isolation eliminates the problem of leftover data or conflicts between tests.
- Cross-Environment Consistency: Since Testcontainers uses Docker, the tests will run the same regardless of the underlying OS or setup. This ensures that your tests pass locally, in CI/CD pipelines, or on any developer's machine.
- Automated Setup and Teardown: Testcontainers automatically handle the lifecycle of Docker containers, starting them before the test and stopping them once the test is complete. This makes the process seamless and ensures a clean environment for each test.
- Avoiding In-Memory Database Limitations: In-memory databases like H2 may not fully replicate the behavior of a real relational database (e.g., differences in SQL syntax or transaction behavior). By using Testcontainers with a real database, we avoid these discrepancies.
Running TestContainer#
- Create the following Test Configuration:
@TestConfiguration(proxyBeanMethods = false) public class TestcontainersConfiguration {
@Bean @ServiceConnection PostgreSQLContainer<?> postgresContainer() { return new PostgreSQLContainer<>(DockerImageName.parse("postgres:latest"));
} }
Code
Create a Configuration class in our test file.
- Import the Configuration in your test file:
@DataJpaTest @Import(TestcontainersConfiguration.class) class EmployeeRepositoryTest {…}
Code
After creating the Configuration class, we need to import it into our test file. Currently, we are not using the H2 database for testing purposes. That's why we use @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
. The H2 database is typically used for lightweight testing, not production. In this case, we are only using Docker for database management.
- Output:
Here, you can see that when your application is running, the status is ‘In use’.
Once the test cases finish, the container stops. In the photo below, you can see the status is ‘Unused’.
Here, we have already covered these aspects, which are highlighted in green in the image.