diff --git a/docs/test_framework_integration/junit_5.md b/docs/test_framework_integration/junit_5.md index 11798484b0f..7f3689c0301 100644 --- a/docs/test_framework_integration/junit_5.md +++ b/docs/test_framework_integration/junit_5.md @@ -23,28 +23,9 @@ executed. Containers declared as instance fields will be started and stopped for unsupported and may have unintended side effects. *Example:* -```java -@Testcontainers -class MyTestcontainersTests { - - // will be shared between test methods - @Container - private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer(); - - // will be started before and stopped after each test method - @Container - private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer() - .withDatabaseName("foo") - .withUsername("foo") - .withPassword("secret"); - @Test - void test() { - assertTrue(MY_SQL_CONTAINER.isRunning()); - assertTrue(postgresqlContainer.isRunning()); - } -} -``` - + +[Mixed Lifecycle](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) inside_block:testClass + ## Examples @@ -55,36 +36,10 @@ To use the Testcontainers extension annotate your test class with `@Testcontaine To define a restarted container, define an instance field inside your test class and annotate it with the `@Container` annotation. -```java -@Testcontainers -class SomeTest { - - @Container - private MySQLContainer mySQLContainer = new MySQLContainer(); - - @Test - void someTestMethod() { - String url = mySQLContainer.getJdbcUrl(); - - // create a connection and run test as normal - } - - @Nested - class NestedTests { + +[Restarted Containers](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java) inside_block:testClass + - @Container - private final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer(); - - void nestedTestMethod() { - // top level container is restarted for nested methods - String mySqlUrl = mySQLContainer.getJdbcUrl(); - - // nested containers are only available inside their nested class - String postgresUrl = postgreSQLContainer.getJdbcUrl(); - } - } -} -``` ### Shared containers @@ -92,21 +47,9 @@ Shared containers are defined as static fields in a top level test class and hav Note that shared containers can't be declared inside nested test classes. This is because nested test classes have to be defined non-static and can't therefore have static fields. -```java -@Testcontainers -class SomeTest { - - @Container - private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer(); - - @Test - void someTestMethod() { - String url = MY_SQL_CONTAINER.getJdbcUrl(); - - // create a connection and run test as normal - } -} -``` + +[Shared Container](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) lines:18-23,32-33,35-36 + ## Singleton containers diff --git a/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java b/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java new file mode 100644 index 00000000000..791c887cc19 --- /dev/null +++ b/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java @@ -0,0 +1,38 @@ +package org.testcontainers.junit.jupiter; + +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.HttpClientBuilder; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.DockerComposeContainer; +import org.testcontainers.containers.MySQLContainer; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.containers.wait.strategy.Wait; + +import static org.junit.jupiter.api.Assertions.assertTrue; + + +// testClass { +@Testcontainers +class MixedLifecycleTests { + + // will be shared between test methods + @Container + private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer(); + + // will be started before and stopped after each test method + @Container + private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer() + .withDatabaseName("foo") + .withUsername("foo") + .withPassword("secret"); + + @Test + void test() { + assertTrue(MY_SQL_CONTAINER.isRunning()); + assertTrue(postgresqlContainer.isRunning()); + } +} +// } diff --git a/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java b/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java index 4d27c410979..fb753744425 100644 --- a/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java +++ b/modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java @@ -8,21 +8,26 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +// testClass { @Testcontainers class TestcontainersNestedRestartedContainerTests { @Container private final GenericContainer topLevelContainer = new GenericContainer("httpd:2.4-alpine") .withExposedPorts(80); + // }} private static String topLevelContainerId; private static String nestedContainerId; + // testClass { @Test void top_level_container_should_be_running() { assertTrue(topLevelContainer.isRunning()); +// }} topLevelContainerId = topLevelContainer.getContainerId(); +// testClass {{ } @Nested @@ -34,16 +39,20 @@ class NestedTestCase { @Test void both_containers_should_be_running() { + // top level container is restarted for nested methods assertTrue(topLevelContainer.isRunning()); + // nested containers are only available inside their nested class assertTrue(nestedContainer.isRunning()); - +// }}} if (nestedContainerId == null) { nestedContainerId = nestedContainer.getContainerId(); } else { assertNotEquals(nestedContainerId, nestedContainer.getContainerId()); } +// testClass {{ } + // } @Test void containers_should_not_be_the_same() { assertNotEquals(topLevelContainer.getContainerId(), nestedContainer.getContainerId()); @@ -65,5 +74,7 @@ void ids_should_not_change() { assertNotEquals(nestedContainerId, nestedContainer.getContainerId()); } } +// testClass {{{ } } +// }