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:activemq becomes org.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.testcontainers dependencies to the 2.x release line.

  • Remove the discontinued org.testcontainers:dynalite dependency, which is no longer published as a module in Testcontainers 2.x.

  • Relocate the container types from the catch-all org.testcontainers.containers package to their corresponding module package. For example, org.testcontainers.containers.PostgreSQLContainerorg.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 Selenium BrowserWebDriverContainer, the AWS LocalStackContainer, and the Google Cloud emulator containers (BigQueryEmulatorContainer, BigtableEmulatorContainer, DatastoreEmulatorContainer, FirestoreEmulatorContainer, PubSubEmulatorContainer, SpannerEmulatorContainer) moving to org.testcontainers.gcloud.

  • Move the Cassandra supporting types CassandraDatabaseDelegate and CassandraQueryWaitStrategy to org.testcontainers.cassandra alongside the container.

  • Migrate the legacy KafkaContainer to the Apache replacement: rewrite confluentinc/cp-kafka images passed to the constructor to apache/kafka-native:latest and insert an inline note pointing teams that want to stay on Confluent at ConfluentKafkaContainer.

  • Consolidate the LocalStack 1.x API onto its 2.x replacement: replace LocalStackContainer.Service enum references with the string names expected by withServices, collapse getEndpointOverride(Service) into the unified no-arg getEndpoint(), and expand bare-tag constructor arguments (such as new 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<?> becomes CassandraContainer).

  • Rename ContainerState.getContainerIpAddress() to getHost().

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