Skip to content

Commit 74a9240

Browse files
raynigonrnorth
andauthored
Convert Docs for the Junit5 (#2977)
Co-authored-by: Richard North <[email protected]>
1 parent 8e57ae6 commit 74a9240

File tree

3 files changed

+59
-67
lines changed

3 files changed

+59
-67
lines changed

docs/test_framework_integration/junit_5.md

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,9 @@ executed. Containers declared as instance fields will be started and stopped for
2323
unsupported and may have unintended side effects.
2424

2525
*Example:*
26-
```java
27-
@Testcontainers
28-
class MyTestcontainersTests {
29-
30-
// will be shared between test methods
31-
@Container
32-
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();
33-
34-
// will be started before and stopped after each test method
35-
@Container
36-
private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
37-
.withDatabaseName("foo")
38-
.withUsername("foo")
39-
.withPassword("secret");
40-
@Test
41-
void test() {
42-
assertTrue(MY_SQL_CONTAINER.isRunning());
43-
assertTrue(postgresqlContainer.isRunning());
44-
}
45-
}
46-
```
47-
26+
<!--codeinclude-->
27+
[Mixed Lifecycle](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) inside_block:testClass
28+
<!--/codeinclude-->
4829

4930
## Examples
5031

@@ -55,58 +36,20 @@ To use the Testcontainers extension annotate your test class with `@Testcontaine
5536
To define a restarted container, define an instance field inside your test class and annotate it with
5637
the `@Container` annotation.
5738

58-
```java
59-
@Testcontainers
60-
class SomeTest {
61-
62-
@Container
63-
private MySQLContainer mySQLContainer = new MySQLContainer();
64-
65-
@Test
66-
void someTestMethod() {
67-
String url = mySQLContainer.getJdbcUrl();
68-
69-
// create a connection and run test as normal
70-
}
71-
72-
@Nested
73-
class NestedTests {
39+
<!--codeinclude-->
40+
[Restarted Containers](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java) inside_block:testClass
41+
<!--/codeinclude-->
7442

75-
@Container
76-
private final PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer();
77-
78-
void nestedTestMethod() {
79-
// top level container is restarted for nested methods
80-
String mySqlUrl = mySQLContainer.getJdbcUrl();
81-
82-
// nested containers are only available inside their nested class
83-
String postgresUrl = postgreSQLContainer.getJdbcUrl();
84-
}
85-
}
86-
}
87-
```
8843

8944
### Shared containers
9045

9146
Shared containers are defined as static fields in a top level test class and have to be annotated with `@Container`.
9247
Note that shared containers can't be declared inside nested test classes.
9348
This is because nested test classes have to be defined non-static and can't therefore have static fields.
9449

95-
```java
96-
@Testcontainers
97-
class SomeTest {
98-
99-
@Container
100-
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();
101-
102-
@Test
103-
void someTestMethod() {
104-
String url = MY_SQL_CONTAINER.getJdbcUrl();
105-
106-
// create a connection and run test as normal
107-
}
108-
}
109-
```
50+
<!--codeinclude-->
51+
[Shared Container](../../modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/MixedLifecycleTests.java) lines:18-23,32-33,35-36
52+
<!--/codeinclude-->
11053

11154
## Singleton containers
11255

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.testcontainers.junit.jupiter;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.client.HttpClient;
5+
import org.apache.http.client.methods.HttpGet;
6+
import org.apache.http.impl.client.HttpClientBuilder;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.testcontainers.containers.DockerComposeContainer;
10+
import org.testcontainers.containers.MySQLContainer;
11+
import org.testcontainers.containers.PostgreSQLContainer;
12+
import org.testcontainers.containers.wait.strategy.Wait;
13+
14+
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
16+
17+
// testClass {
18+
@Testcontainers
19+
class MixedLifecycleTests {
20+
21+
// will be shared between test methods
22+
@Container
23+
private static final MySQLContainer MY_SQL_CONTAINER = new MySQLContainer();
24+
25+
// will be started before and stopped after each test method
26+
@Container
27+
private PostgreSQLContainer postgresqlContainer = new PostgreSQLContainer()
28+
.withDatabaseName("foo")
29+
.withUsername("foo")
30+
.withPassword("secret");
31+
32+
@Test
33+
void test() {
34+
assertTrue(MY_SQL_CONTAINER.isRunning());
35+
assertTrue(postgresqlContainer.isRunning());
36+
}
37+
}
38+
// }

modules/junit-jupiter/src/test/java/org/testcontainers/junit/jupiter/TestcontainersNestedRestartedContainerTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,26 @@
88
import static org.junit.jupiter.api.Assertions.assertNotEquals;
99
import static org.junit.jupiter.api.Assertions.assertTrue;
1010

11+
// testClass {
1112
@Testcontainers
1213
class TestcontainersNestedRestartedContainerTests {
1314

1415
@Container
1516
private final GenericContainer topLevelContainer = new GenericContainer("httpd:2.4-alpine")
1617
.withExposedPorts(80);
18+
// }}
1719

1820
private static String topLevelContainerId;
1921

2022
private static String nestedContainerId;
2123

24+
// testClass {
2225
@Test
2326
void top_level_container_should_be_running() {
2427
assertTrue(topLevelContainer.isRunning());
28+
// }}
2529
topLevelContainerId = topLevelContainer.getContainerId();
30+
// testClass {{
2631
}
2732

2833
@Nested
@@ -34,16 +39,20 @@ class NestedTestCase {
3439

3540
@Test
3641
void both_containers_should_be_running() {
42+
// top level container is restarted for nested methods
3743
assertTrue(topLevelContainer.isRunning());
44+
// nested containers are only available inside their nested class
3845
assertTrue(nestedContainer.isRunning());
39-
46+
// }}}
4047
if (nestedContainerId == null) {
4148
nestedContainerId = nestedContainer.getContainerId();
4249
} else {
4350
assertNotEquals(nestedContainerId, nestedContainer.getContainerId());
4451
}
52+
// testClass {{
4553
}
4654

55+
// }
4756
@Test
4857
void containers_should_not_be_the_same() {
4958
assertNotEquals(topLevelContainer.getContainerId(), nestedContainer.getContainerId());
@@ -65,5 +74,7 @@ void ids_should_not_change() {
6574
assertNotEquals(nestedContainerId, nestedContainer.getContainerId());
6675
}
6776
}
77+
// testClass {{{
6878
}
6979
}
80+
// }

0 commit comments

Comments
 (0)