-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Description
Module
Core
Problem
- For the
org.testcontainers.containers.Networkmentioned in https://java.testcontainers.org/features/networking/ , it always needs to be closed manually by callingNetwork#close(). - Currently, using
org.testcontainers.containers.Networkin junit5 always requires adding an additional@AfterAll. Like the following,
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class SimpleTest {
private static final Network NETWORK = Network.newNetwork();
@Container
private static final GenericContainer<?> ZOOKEEPER_CONTAINER = new GenericContainer<>("zookeeper:3.9.3-jre-17")
.withNetwork(NETWORK)
.withNetworkAliases("foo")
.withExposedPorts(2181);
@Container
public static final GenericContainer<?> OPEN_GAUSS_CONTAINER = new GenericContainer<>("opengauss/opengauss:5.0.0")
.withNetwork(NETWORK)
.withEnv("GS_PASSWORD", "openGauss@123")
.withExposedPorts(5432);
@AfterAll
static void afterAll() {
NETWORK.close();
}
@Test
void test() {
ZOOKEEPER_CONTAINER.getMappedPort(2181);
OPEN_GAUSS_CONTAINER.getMappedPort(5432);
}
}Solution
- I would like to avoid using
org.junit.jupiter.api.AfterAllas follows.
import org.junit.jupiter.api.Test;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.Network;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
public class SimpleTest {
@org.testcontainers.junit.jupiter.Network
private static final Network NETWORK = Network.newNetwork();
@Container
private static final GenericContainer<?> ZOOKEEPER_CONTAINER = new GenericContainer<>("zookeeper:3.9.3-jre-17")
.withNetwork(NETWORK)
.withNetworkAliases("foo")
.withExposedPorts(2181);
@Container
public static final GenericContainer<?> OPEN_GAUSS_CONTAINER = new GenericContainer<>("opengauss/opengauss:5.0.0")
.withNetwork(NETWORK)
.withEnv("GS_PASSWORD", "openGauss@123")
.withExposedPorts(5432);
@Test
void test() {
ZOOKEEPER_CONTAINER.getMappedPort(2181);
OPEN_GAUSS_CONTAINER.getMappedPort(5432);
}
}- It would be nice if an annotation like
@org.testcontainers.junit.jupiter.Containercould be added to theorg.testcontainers.containers.Networkclass. Maybe the class name is@org.testcontainers.junit.jupiter.Network.
Benefit
- Simplifies use.
Alternatives
- Just to align the usage of Junit4 on Junit5. This is a derivative of Support connecting to HiveServer2 with ZooKeeper Service Discovery enabled in GraalVM Native Image apache/shardingsphere#33768 and Improve GraalVM Reachability Metadata and corresponding nativeTest related unit tests apache/shardingsphere#29052 .
Would you like to help contributing this feature?
No