Testcontainers 2
When upgrading Testcontainers to a newer version, refer to the Testcontainers 2.0 Release Notes to find out about newly introduced APIs, deprecated APIs, and information about breaking changes.
The io.arconia.rewrite.test.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, …), 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. -
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. -
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().
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.test.testcontainers.UpgradeTestcontainers_2
First, create an init.gradle file in your project root with the following content.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies {
classpath("org.openrewrite:plugin:latest.release")
}
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("io.arconia.migrations:rewrite-test:latest.release")
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
configurations.named("rewrite") {
canBeConsumed = false
}
}
Then, run the following command.
./gradlew rewriteRun \
--init-script init.gradle \
-DactiveRecipe=io.arconia.rewrite.test.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-test:LATEST \
-Drewrite.activeRecipes=io.arconia.rewrite.test.testcontainers.UpgradeTestcontainers_2