Testcontainers is a lightweight Java library for anything that can run in a Docker container
Looking for a Java library to support your JUnit tests? Introducing Testcontainers, a lightweight library for anything that can run in a container. Launch useful Docker containers for the duration of JUnit tests today!
We like to go through underappreciated Java libraries on the regular to shine a light on some gems. Today, we’re taking a look at Testcontainers, a Java library that supports JUnit tests. Now, developers can easily launch useful Docker containers full of whatever they need during their JUnit tests.
Testcontainers provides lightweight, throwaway instances of anything that can be stuffed into a container. This makes using Docker containers even easier than ever for testing purposes.
So, what does Testcontainers offer?
- Data access layer integration tests: Developers can use a containerized instance of their favorite database to test their data access lay code for complete compatibility. No complex setup process necessary!
- Application integration tests: Need a disposable test mode? Developers can run their app in a short-lived test mode with dependencies.
- UI/Acceptance tests: Developers can use containerized web browsers for conducting automated UI tests. Each test can get a fresh instance of the browser, with no browser state, plugin variations or automated browser upgrades to worry about. There’s even a video recording of each test session or just of failed sessions, to make sure everything goes smoothly.
Testcontainers is already used by a number of organizations. This open source Java library generally requires Docker v1.10 or above to work.
The Testcontainers 1.10 release comes with loads of new features and use cases.
To start, there is now official Junit 5 support. This has been a long time coming and is provided though the @Testcontainers annotation. Basically, the extension finds all of the fields that are annotated with @Container. It then calls their container lifecycle methods (methods on the Startable interface). Any containers declared as static fields will be shared between test methods. They’ll be started once before any test methods are executed and stopped after the last test method has executed. Containers declared as instance fields will be started and stopped for every test method.
Additionally, in Testcontainers 1.10 there is now an Elasticsearch container class. This makes testing against elasticsearch easier than ever. It’s based off the official Docker image provided by elastic.
Developers can also test against OpenJDK 11, support empty passwords for MySQL, and activate Gradle build scans for CI builds. The 1.10 release also comes with a number of documentation improvements, support for new features, and updates. Check out the full changelog for all the details.
Want to try this Java library out? The latest version of Testcontainers is available on GitHub. For more information about this cool tool, head on over to the Testcontainers site for documentation and more.