Test Containers Tutorial
Introduction to Test Containers
Test Containers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web drivers, and anything else that can run in a Docker container. This is particularly useful for integration testing, where you want to ensure that your application interacts with external systems (like databases) as expected.
Why Use Test Containers?
Using Test Containers allows you to:
- Run tests in isolation with a clean state.
- Simulate production environments more accurately.
- Reduce flakiness in tests caused by environmental issues.
- Easily manage dependencies like databases without manual setup.
Setting Up Test Containers
Before using Test Containers in your project, you need to add the required dependencies. If you are using Maven, add the following to your pom.xml:
<dependency> <groupId>org.testcontainers</groupId> <artifactId>testcontainers</artifactId> <version>1.17.3</version> <scope>test</scope> </dependency>
For Gradle, add this to your build.gradle:
testImplementation 'org.testcontainers:testcontainers:1.17.3'
Creating a Simple Test with Test Containers
Here's an example of how to use Test Containers to test a Hibernate application with a PostgreSQL database. First, you need to create a test class and annotate it with @Testcontainers.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Testcontainers; @Testcontainers public class UserRepositoryTest { static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:latest") .withDatabaseName("test") .withUsername("user") .withPassword("password"); @Test public void testSaveUser() { // Your test logic here } }
In this example, a PostgreSQL container is created automatically before each test. The database can be accessed with the specified username and password in your Hibernate configuration.
Configuring Hibernate with Test Containers
To connect Hibernate with the Test Containers PostgreSQL instance, you'll need to configure your hibernate.cfg.xml or equivalent configuration class as follows:
<hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://<?= postgres.getContainerIpAddress() >:<?= postgres.getMappedPort(5432) >/test</property> <property name="hibernate.connection.username">user</property> <property name="hibernate.connection.password">password</property> </session-factory> </hibernate-configuration>
Running Your Tests
You can run your tests using your IDE or via the command line. If using Maven, run the following command:
mvn test
If you're using Gradle, use:
gradle test
The Test Containers will spin up the necessary Docker containers, run your tests, and then shut down the containers automatically.
Conclusion
Test Containers provide a powerful way to integrate and test applications that interact with external systems. By leveraging Docker, they ensure that your tests run in a clean environment, minimizing issues related to dependencies and configurations. This approach leads to more reliable tests and ultimately better software quality.