1

I have integration tests (cucumber) which I use a wiremock server with port 8085 and when I run the test execution class everything works, I added another class to test a service of which I to use another WirkeMock server instance with the same port it is running successfully.

@RunWith(SpringRunner.class)
@SpringBootTest
public class ClasseCucumber {


    private WireMockRule wiremockRule = new WireMockRule(8085); 

    @Before
    public void beforeScenario() throws IOException {

        wiremockRule.start();
       WireMock.configureFor("localhost", wiremockRule.port());

        stubFor(post(urlEqualTo(endpoint))
                .withRequestBody(equalToJson(requete))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "application/json")
                        .withBody(response)
                        .withStatus(HttpStatus.OK.value())));
    }

    @After
    public void afterScenario() {
             wiremockRule.stop();
    }

}
@RunWith(SpringRunner.class)
@SpringBootTest
public class ServiceTest {

    private static WireMockRule wiremock = new WireMockRule(8085); 

    @BeforeClass
    public static void setUp() throws IOException {

        wiremock.start();
            WireMock.configureFor("localhost", wiremock.port());

        stubFor(post(urlEqualTo(endpoint))
                .withRequestBody(equalToJson(requete))
                .willReturn(aResponse()
                        .withHeader("Content-Type", "application/json")
                        .withBody(response)
                        .withStatus(HttpStatus.OK.value())));


    }

    @AfterClass
    public static void afterTest() {

        wiremock.stop();
    }

    @Test
    public void appelTest() {
   [...]
    }

}

My problem when I make a clean mvn install (which launches all the tests) I have this error:

om.github.tomakehurst.wiremock.common.FatalStartupException: java.lang.RuntimeException: java.net.BindException: Adresse déjà utilisée at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:147) at fr.pe.rind.service.da058.certification.web.CertificationStepdefs.beforeScenario(CertificationStepdefs.java:52) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at cucumber.runtime.Utils$1.call(Utils.java:32) at cucumber.runtime.Timeout.timeout(Timeout.java:16) at cucumber.runtime.Utils.invoke(Utils.java:26) at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:60) at cucumber.runtime.HookDefinitionMatch.runStep(HookDefinitionMatch.java:17) at cucumber.runner.UnskipableStep.executeStep(UnskipableStep.java:22) at cucumber.api.TestStep.run(TestStep.java:83) at cucumber.api.TestCase.run(TestCase.java:58) at cucumber.runner.Runner.runPickle(Runner.java:80) at cucumber.runtime.junit.PickleRunners$NoStepDescriptions.run(PickleRunners.java:140) at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68) at cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73) at cucumber.api.junit.Cucumber.runChild(Cucumber.java:117) at cucumber.api.junit.Cucumber.runChild(Cucumber.java:55) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at cucumber.api.junit.Cucumber$1.evaluate(Cucumber.java:126) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365) at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159) at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344) at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417) Caused by: java.lang.RuntimeException: java.net.BindException: Adresse déjà utilisée at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:165) at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:145)

2 Answers 2

1

Running multiple applications on the same port is not possible. This is not a limitation of WireMock but a generic OS/Application one.

1
  • Yes, but is there a way to find another port available to be used? ``` val apiAServer = WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort()) val apiBServer = WireMockServer(????) ```
    – bh4r4th
    Commented Feb 3, 2023 at 2:39
0

You could use a dynamic port, with the below code

int port = org.springframework.util.SocketUtils.findAvailableTcpPort();

WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(port));
wireMockServer.start();

Not the answer you're looking for? Browse other questions tagged or ask your own question.