Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Add an annotation similar to @org.testcontainers.junit.jupiter.Container to the org.testcontainers.containers.Network class #9552

Open
linghengqian opened this issue Nov 23, 2024 · 0 comments

Comments

@linghengqian
Copy link

linghengqian commented Nov 23, 2024

Module

Core

Problem

  • For the org.testcontainers.containers.Network mentioned in https://java.testcontainers.org/features/networking/ , it always needs to be closed manually by calling Network#close().
  • Currently, using org.testcontainers.containers.Network in 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.AfterAll as 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.Container could be added to the org.testcontainers.containers.Network class. Maybe the class name is @org.testcontainers.junit.jupiter.Network.

Benefit

  • Simplifies use.

Alternatives

Would you like to help contributing this feature?

No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants