4
4
5
5
import com .github .tomakehurst .wiremock .WireMockServer ;
6
6
import io .restassured .RestAssured ;
7
+ import org .junit .jupiter .api .AfterEach ;
7
8
import org .slf4j .LoggerFactory ;
8
9
import org .testcontainers .Testcontainers ;
9
10
import org .testcontainers .containers .GenericContainer ;
14
15
import org .testcontainers .lifecycle .Startable ;
15
16
import org .testcontainers .utility .MountableFile ;
16
17
17
- abstract class AbstractContainerBaseTest {
18
+ abstract class DefaultContainerStarterTest {
18
19
19
20
private static final GenericContainer <?> APP ;
20
21
private static final GenericContainer <?> FLYWAY ;
21
22
private static final GenericContainer <?> MYSQL_CONTAINER ;
23
+ private static final Network NETWORK = Network .newNetwork ();
22
24
protected static final WireMockServer MOCK_SERVER ;
23
25
26
+ /* Containers are initialized in static block to create only once in test execution */
24
27
static {
25
- final var network = Network .newNetwork ();
26
-
27
28
MOCK_SERVER = new WireMockServer (wireMockConfig ().dynamicPort ());
28
29
MOCK_SERVER .start ();
29
30
exposeHostMachinePortToContainersForApiIntegrations ();
30
31
31
- MYSQL_CONTAINER = buildMySqlContainer (network );
32
+ MYSQL_CONTAINER = buildMySqlContainer ();
32
33
MYSQL_CONTAINER .start ();
33
34
34
- FLYWAY = buildFlywayContainer (network , MYSQL_CONTAINER );
35
+ FLYWAY = buildFlywayContainer (MYSQL_CONTAINER );
35
36
FLYWAY .start ();
36
37
37
- APP = buildAppContainer (network , MYSQL_CONTAINER , FLYWAY );
38
+ APP = buildAppContainer (MYSQL_CONTAINER , FLYWAY );
38
39
APP .start ();
39
40
40
41
initRestAssured ();
41
42
}
42
43
43
- private static GenericContainer <?> buildMySqlContainer (final Network network ) {
44
+ private static GenericContainer <?> buildMySqlContainer () {
44
45
return new MySQLContainer <>("mysql:5.7.22" )
45
- .withNetwork (network )
46
+ .withNetwork (NETWORK )
46
47
.withNetworkAliases ("testdb" );
47
48
}
48
49
49
- private static GenericContainer <?> buildFlywayContainer (final Network network , final Startable ... dependsOn ) {
50
+ private static GenericContainer <?> buildFlywayContainer (final Startable ... dependsOn ) {
50
51
return new GenericContainer <>("flyway/flyway" )
51
52
.dependsOn (dependsOn )
52
- .withNetwork (network )
53
+ .withNetwork (NETWORK )
53
54
.withCopyFileToContainer (MountableFile .forHostPath ("../resources/flyway/db" ), "/flyway/sql" )
54
55
.withCommand ("-url=jdbc:mysql://testdb?useSSL=false -schemas=test -user=test -password=test -connectRetries=60 migrate" )
55
56
.waitingFor (Wait .forLogMessage ("(?s).*No migration necessary(?s).*|(?s).*Successfully applied(?s).*" , 1 ))
56
57
.withLogConsumer (new Slf4jLogConsumer (LoggerFactory .getLogger ("FLYWAY" )));
57
58
}
58
59
59
- private static GenericContainer <?> buildAppContainer (final Network network , final Startable ... dependsOn ) {
60
+ private static GenericContainer <?> buildAppContainer (final Startable ... dependsOn ) {
60
61
return new GenericContainer <>("app-test:integration" )
61
62
.dependsOn (dependsOn )
62
- .withNetwork (network )
63
+ .withNetwork (NETWORK )
63
64
.withEnv ("RANDOM_DATA_API_URL" , "http://host.testcontainers.internal:" + MOCK_SERVER .port ())
64
65
.withEnv ("MYSQL_USER" , "test" )
65
66
.withEnv ("MYSQL_PASSWORD" , "test" )
@@ -80,4 +81,10 @@ private static void exposeHostMachinePortToContainersForApiIntegrations() {
80
81
Testcontainers .exposeHostPorts (MOCK_SERVER .port ());
81
82
}
82
83
84
+ @ AfterEach
85
+ void tearDown () {
86
+ MOCK_SERVER .resetAll ();
87
+ /* add here others resets needed after each test */
88
+ }
89
+
83
90
}
0 commit comments