AtomicJar has introduced the freely available Testcontainers Desktop, a new application that supports Testcontainers, an open source framework which allows the use of real dependencies in unit tests. Testcontainers Desktop supports several features that aren't available in the Testcontainers framework and offers better support for investigations and debugging with fixed ports for containers and the option to freeze and reuse containers. Lastly, the application supports switching easily between container runtimes.
In November 2021, AtomicJar launched a new product, Testcontainers Cloud, in public beta which allows teams to run containers on a range of different environments and configurations. It required a desktop application, which resulted in Testcontainers Cloud Desktop. Now the TestContainers Cloud Desktop tool is donated to the community as the free Testcontainers Desktop application.
Testcontainers Desktop allows setting fixed ports instead of the default random ports used by Testcontainers. Containers with fixed ports are easier when debugging from, for example, IntelliJ. Freeze containers prevents the shutdown of a container and makes the test run forever. This makes it easier to investigate and debug. The freeze button may be unchecked after the analyses, to resume the test execution and default cleanup logic of Testcontainers. The application also allows switching the container runtime and supports solutions such as OrbStack, Colima, Rancher Desktop and Podman.
The Testcontainers framework makes it possible to write tests with JUnit that use databases, message brokers and any other service in a Docker container.
First the testcontainers BOM should be configured in the pom.xml:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers-bom</artifactId>
<version>1.19.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
After that, the dependencies may be used without specifying the version, for example for MySQL:
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<scope>test</scope>
</dependency>
Alternatively, Gradle may be used to manage the dependencies:
implementation platform('org.testcontainers:testcontainers-bom:1.19.0') testImplementation('org.testcontainers:mysql')
Now the Docker container may be injected into JUnit tests. For example by injecting the MySQL container with the defaults value test for the database, username and password:
@Container
private static final MySQLContainer mySQLContainer = new MySQLContainer<>("mysql:8.1.0");
Alternatively, the default values may be overwritten with custom values:
@Container
private static final MySQLContainer mySQLContainer = new MySQLContainer<>("mysql:8.1.0")
.withDatabaseName("myDatabase")
.withUsername("myUsername")
.withPassword("myPassword");
Testcontainers provide various modules which facilitate the integration of those Docker containers. Alternatively, any Docker container may be used by using the GenericContainer
class:
@Container
private static final GenericContainer myContainer = new GenericContainer("redis:7.2.1")
.withExposedPorts(42);
Finally, the container may be used inside the unit test to create a connection to the service and execute the unit test logic.
The documentation provides more information about Testcontainers for Java.
AtomicJar released a short demo video introducing Testcontainers Desktop. Testcontainers Desktop binaries are available for installation on Mac, Windows and Linux operating systems.
The Simple local development with Testcontainers Desktop guide provides more information. Next to that, Oleg Selajev, lead developer advocate at AtomicJar and Sergei Egorov, co-founder and CEO at AtomicJar, discussed and demonstrated Testcontainers Desktop on YouTube.