Testcontainers 2
When upgrading to Testcontainers 2, refer to the Testcontainers 2.0 Release Notes for newly introduced APIs, deprecated APIs, and breaking changes.
The io.arconia.rewrite.testing.testcontainers.UpgradeTestcontainers_2 recipe automates most of the changes introduced in Testcontainers 2.x. You can use it to update your project to the latest Testcontainers 2.x release.
The recipe will:
-
Rename the Testcontainers module artifacts to the new
testcontainers-<module>naming scheme (for example,org.testcontainers:activemqbecomesorg.testcontainers:testcontainers-activemq). This applies to all the supported modules, including ActiveMQ, Azure, Cassandra, ChromaDB, ClickHouse, CockroachDB, Consul, Couchbase, the JDBC drivers (Db2, MariaDB, MS SQL Server, MySQL, Oracle Free/XE, PostgreSQL, and more), the cloud SDKs (AWS LocalStack, Azure, Google Cloud), Elasticsearch, Kafka, MongoDB, Neo4j, RabbitMQ, Redpanda, Selenium, and many more. -
Upgrade all
org.testcontainersdependencies to the 2.x release line. -
Remove the discontinued
org.testcontainers:dynalitedependency, which is no longer published as a module in Testcontainers 2.x. -
Relocate the container types from the catch-all
org.testcontainers.containerspackage to their corresponding module package. For example,org.testcontainers.containers.PostgreSQLContainer→org.testcontainers.postgresql.PostgreSQLContainer. The same move applies to the Cassandra, ClickHouse, CockroachDB, Db2, Kafka, MariaDB, MockServer, MongoDB, MSSQLServer, MySQL, Neo4j, Nginx, OrientDB, Pulsar, RabbitMQ, Solr, Toxiproxy, and Trino container types, the SeleniumBrowserWebDriverContainer, the AWSLocalStackContainer, and the Google Cloud emulator containers (BigQueryEmulatorContainer,BigtableEmulatorContainer,DatastoreEmulatorContainer,FirestoreEmulatorContainer,PubSubEmulatorContainer,SpannerEmulatorContainer) moving toorg.testcontainers.gcloud. -
Move the Cassandra supporting types
CassandraDatabaseDelegateandCassandraQueryWaitStrategytoorg.testcontainers.cassandraalongside the container. -
Migrate the legacy
KafkaContainerto the Apache replacement: rewriteconfluentinc/cp-kafkaimages passed to the constructor toapache/kafka-native:latestand insert an inline note pointing teams that want to stay on Confluent atConfluentKafkaContainer. -
Consolidate the LocalStack 1.x API onto its 2.x replacement: replace
LocalStackContainer.Serviceenum references with the string names expected bywithServices, collapsegetEndpointOverride(Service)into the unified no-arggetEndpoint(), and expand bare-tag constructor arguments (such asnew LocalStackContainer("0.11.2")) into full image names so the 2.x constructor pulls the right image. -
Drop the self-referential generic type parameter from the relocated container types so they can be used as raw types (for example,
CassandraContainer<?>becomesCassandraContainer). -
Rename
ContainerState.getContainerIpAddress()togetHost().
See its entry in the Recipe Catalog for the full list of recipes this upgrade applies, each with its options and a before/after example.
Run the Recipe
You can apply this recipe with any OpenRewrite-compatible tool. The tabs below show three of them: the Arconia CLI, the OpenRewrite Gradle plugin, and the OpenRewrite Maven plugin. See Running Recipes for a comparison of when to pick each one.
-
Arconia CLI
-
Gradle
-
Maven
arconia rewrite run --recipe-name io.arconia.rewrite.testing.testcontainers.UpgradeTestcontainers_2
First, create an init.gradle file in your project root with the following content.
initscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath("org.openrewrite:plugin:latest.release")
}
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("io.arconia.migrations:rewrite-testing:latest.release")
}
// Remove when project repositories are disabled
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
configurations.named("rewrite") {
canBeConsumed = false
}
}
Then, run the following command.
./gradlew rewriteRun \
--init-script init.gradle \
--no-parallel \
-DactiveRecipe=io.arconia.rewrite.testing.testcontainers.UpgradeTestcontainers_2
You can remove the init.gradle file afterwards.
./mvnw -U org.openrewrite.maven:rewrite-maven-plugin:run \
-Drewrite.recipeArtifactCoordinates=io.arconia.migrations:rewrite-testing:LATEST \
-Drewrite.activeRecipes=io.arconia.rewrite.testing.testcontainers.UpgradeTestcontainers_2