Wrong port used on Junit Jupiter nested classes
Version : 2.33.2
I have noticed two behaviours that don't seem to work as expected regarding nested classes and wiremock server ports.
1. The server is not restarted on the first nested class, even if the port is not the same
The first issue can be reproduced using a simplified version of wiremock unit test class JunitJupiterExtensionDeclarativeWithConfiguredNestedTest:
@WireMockTest(httpPort = 8765)
class JunitJupiterExtensionDeclarativeWithConfiguredNestedTest {
@Test
void runs_on_the_supplied_port(WireMockRuntimeInfo wmRuntimeInfo) {
assertThat(wmRuntimeInfo.getHttpPort(), is(8765));
}
@Nested
@WireMockTest(httpPort = 8766)
class RunsOn8766 {
@Test
void runs_on_the_supplied_port(WireMockRuntimeInfo wmRuntimeInfo) {
assertThat(wmRuntimeInfo.getHttpPort(), is(8766));
}
}
}When running the test in RunsOn8766 class the wiremock server is not restarted, therefore it is still running on port 8765 causing the error :
java.lang.AssertionError:
Expected: is <8766>
but: was <8765>2. The server is restarted on a random port on second nested class
The second issue can also be reproduced by changing a little bit this same test class and forcing an order on the tests :
@WireMockTest(httpPort = 8765)
@TestClassOrder(ClassOrderer.OrderAnnotation.class)
class JunitJupiterExtensionDeclarativeWithConfiguredNestedTest {
@Test
void runs_on_the_supplied_port(WireMockRuntimeInfo wmRuntimeInfo) {
assertThat(wmRuntimeInfo.getHttpPort(), is(8765));
}
@Nested
@Order(0)
class RunsOnInheritedPortFirstTry {
@Test
void runs_on_the_supplied_port(WireMockRuntimeInfo wmRuntimeInfo) {
assertThat(wmRuntimeInfo.getHttpPort(), is(8765));
}
}
@Nested
@Order(1)
class RunsOnInheritedPortSecondTry {
@Test
void runs_on_the_supplied_port(WireMockRuntimeInfo wmRuntimeInfo) {
assertThat(wmRuntimeInfo.getHttpPort(), is(8765));
}
}
}When running the test in RunsOnInheritedPortSecondTry class the wiremock server on 8765 has already been stopped (at the end of RunsOnInheritedPortFirstTry class). But when it restarts, it will restart on a random port and won't be able to retrieve the annotation on the parent class, causing an error like this :
java.lang.AssertionError:
Expected: is <8765>
but: was <55464>I have tried to fix the code myself and open a PR but it can be tricky because at some point we might have a server already running on 8765 and not stopped yet, then one test in a nested class restarting a server on another port, and then a second test on a second nested class that needs to reuse the first server on 8765. I didn't know how to retrieve that one and couldn't start a second server on the same port for obvious reasons.
Source: wiremock/wiremock