Skip to content

Commit bfc0166

Browse files
Merge pull request #171 from TheRealGuru/hotfix-port-override
fix: Use addExposedPort instead of setExposedPort which overrides user configuration
2 parents b61b557 + c76c5fb commit bfc0166

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

src/main/java/org/wiremock/integrations/testcontainers/WireMockContainer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,18 @@ public class WireMockContainer extends GenericContainer<WireMockContainer> {
6363
private static final String FILES_DIR = "/home/wiremock/__files/";
6464

6565
private static final String EXTENSIONS_DIR = "/var/wiremock/extensions/";
66+
private static final int PORT = 8080;
6667
private static final WaitStrategy DEFAULT_WAITER = Wait
6768
.forHttp("/__admin/mappings")
6869
.withMethod("GET")
69-
.forStatusCode(200);
70+
.forStatusCode(200)
71+
.forPort(PORT);
7072

7173
private static final WaitStrategy HEALTH_CHECK_ENDPOINT_WAITER = Wait
7274
.forHttp("/__admin/health")
7375
.withMethod("GET")
74-
.forStatusCode(200);
75-
private static final int PORT = 8080;
76+
.forStatusCode(200)
77+
.forPort(PORT);
7678
private final StringBuilder wireMockArgs;
7779
private final Map<String, Stub> mappingStubs = new HashMap<>();
7880
private final Map<String, MountableFile> mappingFiles = new HashMap<>();
@@ -371,7 +373,7 @@ public Integer getPort() {
371373
@Override
372374
protected void configure() {
373375
super.configure();
374-
withExposedPorts(PORT);
376+
addExposedPorts(PORT);
375377
for (Stub stub : mappingStubs.values()) {
376378
withCopyToContainer(Transferable.of(stub.json), MAPPINGS_DIR + stub.name + ".json");
377379
}

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerJunit4Test.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
public class WireMockContainerJunit4Test {
2626

27+
private static final int WIREMOCK_DEFAULT_PORT = 8080;
28+
private static final int ADDITIONAL_MAPPED_PORT = 8443;
29+
2730
@Rule
2831
public WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
2932
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
3033
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
31-
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, "hello-world-resource-response.xml");
34+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class, "hello-world-resource-response.xml")
35+
.withExposedPorts(ADDITIONAL_MAPPED_PORT);
3236

3337
@Test
3438
public void helloWorld() throws Exception {
@@ -71,4 +75,15 @@ public void helloWorldFromFile() throws Exception {
7175
.as("Wrong response body")
7276
.contains("Hello, world!");
7377
}
78+
79+
@Test
80+
public void customPortsAreExposed() {
81+
//given
82+
83+
//when
84+
85+
//then
86+
assertThat(wiremockServer.getExposedPorts())
87+
.contains(WIREMOCK_DEFAULT_PORT, ADDITIONAL_MAPPED_PORT);
88+
}
7489
}

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerTest.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
@Testcontainers(parallel = true)
2929
class WireMockContainerTest {
3030

31+
private static final int WIREMOCK_DEFAULT_PORT = 8080;
32+
private static final int ADDITIONAL_MAPPED_PORT = 8443;
33+
3134
@Container
3235
WireMockContainer wiremockServer = new WireMockContainer(TestConfig.WIREMOCK_DEFAULT_IMAGE)
3336
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
3437
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
3538
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
36-
"hello-world-resource-response.xml");
39+
"hello-world-resource-response.xml")
40+
.withExposedPorts(ADDITIONAL_MAPPED_PORT);
3741

3842

3943
@ParameterizedTest
@@ -67,4 +71,15 @@ void helloWorldFromFile() throws Exception {
6771
.as("Wrong response body")
6872
.contains("Hello, world!");
6973
}
74+
75+
@Test
76+
void customPortsAreExposed() {
77+
//given
78+
79+
//when
80+
81+
//then
82+
assertThat(wiremockServer.getExposedPorts())
83+
.contains(WIREMOCK_DEFAULT_PORT, ADDITIONAL_MAPPED_PORT);
84+
}
7085
}

0 commit comments

Comments
 (0)