public class MySQLContainerTest {
@Container
private static final MySQLContainer<?> mysqlContainer = new MySQLContainer<>("mysql:8.0.26")
.withDatabaseName("test")
.withUsername("user")
.withPassword("password");
@Test
public void testMySQLContainer() {
}
}
public class CustomContainerTest {
@ClassRule
public static final GenericContainer<?> customContainer = new GenericContainer<>("my-custom-image:latest")
.withExposedPorts(8080)
.withEnv("ENV_VAR", "value");
@Test
public void testCustomContainer() {
String containerHost = customContainer.getContainerIpAddress();
Integer containerPort = customContainer.getMappedPort(8080);
}
}
public class LinkedContainersTest {
@ClassRule
public static final SharedNetworkContainer sharedNetwork = new SharedNetworkContainer();
@ClassRule
public static final GenericContainer<?> redisContainer = new GenericContainer<>("redis:6.2.5")
.withNetwork(sharedNetwork.getNetwork())
.withExposedPorts(6379);
@ClassRule
public static final GenericContainer<?> postgresContainer = new GenericContainer<>("postgres:13.4")
.withNetwork(sharedNetwork.getNetwork())
.withExposedPorts(5432);
@Test
public void testLinkedContainers() {
String redisHost = redisContainer.getContainerIpAddress();
Integer redisPort = redisContainer.getMappedPort(6379);
String postgresHost = postgresContainer.getContainerIpAddress();
Integer postgresPort = postgresContainer.getMappedPort(5432);
}
}