@@ -23,28 +23,9 @@ executed. Containers declared as instance fields will be started and stopped for
23
23
unsupported and may have unintended side effects.
24
24
25
25
* 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-->
48
29
49
30
## Examples
50
31
@@ -55,58 +36,20 @@ To use the Testcontainers extension annotate your test class with `@Testcontaine
55
36
To define a restarted container, define an instance field inside your test class and annotate it with
56
37
the ` @Container ` annotation.
57
38
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-->
74
42
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
- ```
88
43
89
44
### Shared containers
90
45
91
46
Shared containers are defined as static fields in a top level test class and have to be annotated with ` @Container ` .
92
47
Note that shared containers can't be declared inside nested test classes.
93
48
This is because nested test classes have to be defined non-static and can't therefore have static fields.
94
49
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-->
110
53
111
54
## Singleton containers
112
55
0 commit comments