Skip to content

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

@linghengqian

Description

@linghengqian

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions