WIP:integrationtest
[demos/testing] / src / test / java / de / juplo / demo / IntegrationTestBase.java
1 package de.juplo.demo;
2
3
4 import org.junit.jupiter.api.AfterAll;
5 import org.junit.jupiter.api.BeforeAll;
6 import org.mockserver.integration.ClientAndServer;
7 import org.testcontainers.containers.NginxContainer;
8 import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;
9
10
11
12 /**
13  * This class starts a
14  * {@link https://www.testcontainers.org/modules/nginx/ NginX-Webserver}
15  * via a {@link https://www.testcontainers.org/ Testcontainer} and a
16  * standalone {@link http://www.mock-server.com/#what-is-mockserver MockServer},
17  * that can be used to intercept requests for assertions / verifications.
18  * <p>
19  * We use the {@link
20  * https://www.testcontainers.org/test_framework_integration/manual_lifecycle_control/#singleton-containers
21  * Singleton Pattern} to start the NginX-Testcontainer and start the MockServer
22  * programmatically via the {@link
23  * http://www.mock-server.com/mock_server/running_mock_server.html#client_api
24  * Client-API}.
25  * @author Kai Moritz
26  */
27 public abstract class IntegrationTestBase
28 {
29   static final NginxContainer NGINX;
30   static final ClientAndServer MOCK_SERVER;
31
32
33   static
34   {
35     NGINX = new NginxContainer().withCustomContent("src/test/resources/");
36     MOCK_SERVER = ClientAndServer.startClientAndServer();
37   }
38
39
40   @BeforeAll
41   static void startMockServer()
42   {
43     NGINX.waitingFor(new HttpWaitStrategy()).start();
44   }
45
46   @AfterAll
47   static void stopMockServer()
48   {
49     MOCK_SERVER.stop();
50   }
51
52
53   String getNginxIP()
54   {
55     return NGINX.getContainerIpAddress();
56   }
57
58   int getNginxPort()
59   {
60     return NGINX.getMappedPort(80);
61   }
62
63   String getNginxUri()
64   {
65     return "http://" + getNginxIP() + ":" + getNginxPort();
66   }
67
68   int getMockServerPort()
69   {
70     return MOCK_SERVER.getLocalPort();
71   }
72
73   String getMockServerUri()
74   {
75     return "http://localhost:" + getMockServerPort();
76   }
77 }