The Past, the Present and the Future
GenericContainer redis =
new GenericContainer("redis:3.0.2")
.withExposedPorts(6379);
redis.start();
// test my stuff
redis.stop();
@whichrich
@bsideup
@Testcontainers
class SomeTest {
@Container
private MySQLContainer mySQLContainer = new MySQLContainer();
@Test
void someTestMethod() {
String url = mySQLContainer.getJdbcUrl();
// create a connection and run test as normal
}
}
Testcontainers.exposeHostPorts(localServerPort);
final String rootUrl =
String.format("http://host.testcontainers.internal:%d/", localServerPort);
You can see an example test that could
have been written for it (without using Testcontainers):
<!--codeinclude-->
[Pre-Testcontainers test code]
(../examples/junit5/redis/src/test/java/quickstart/RedisBackedCacheIntTestStep0.java)
block:RedisBackedCacheIntTestStep0
<!--/codeinclude-->
GenericContainer container = new GenericContainer()
.dependsOn(postgresContainer, kafkaContainer);
new GenericContainer<>(imageName)
.withImagePullPolicy(PullPolicy.alwaysPull())
AgeBasedPullPolicy oneHour = new AgeBasedPullPolicy(
Duration.of(1L, ChronoUnit.HOURS)
);
new GenericContainer<>(imageName)
.withImagePullPolicy(oneHour)
public DockerComposeContainer environment =
new DockerComposeContainer(new File("src/test/resources/compose-test.yml"))
.withExposedService("redis_1", REDIS_PORT)
.withExposedService("db_1", 3306);
Optional<ContainerState> result = environment.getContainerByServiceName("db_1");
r2dbc:tc:mysql:///databasename?TC_IMAGE_TAG=5.7.22
// deprecrate
new XyzContainer()
new XyzContainer(String)
// in favor for
new XyzContainer(DockerImageName)
// allowing
new XyzContainer( DockerImageName.parse( "the/image:tag" ) )
// will just work
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:any"));
// will initially fail
new KafkaContainer(DockerImageName.parse("some-other-kafka"));
// be explicit instead!
new KafkaContainer(DockerImageName.parse("some-other-kafka"))
.asCompatibleSubstituteFor("confluentinc/cp-kafka");
private GenericContainer myContainer = new GenericContainer("myImage:42.23") {{
withExposedPorts(4711);
if (System.getenv().get("INSIDE_CI") == null) {
org.testcontainers.Testcontainers.exposeHostPorts(8080);
}
}};
val redisContainer = GenericContainer<Nothing>("redis:3-alpine")
.apply {
withExposedPorts(6379)
}
TODO: add Safe Harbor Statement here