如何在同一端口上使用多个Wiremock实例运行多个测试?

时间:2019-05-22 14:54:42

标签: spring-boot cucumber wiremock

我有集成测试(黄瓜),我使用带有8085端口的Wiremock服务器,当我运行测试执行类时,一切正常,我添加了另一个类来测试一项服务,该服务将使用相同的另一个WirkeMock服务器实例端口成功运行。

@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() {
   [...]
    }

}

我进行干净的mvn安装(将启动所有测试)时出现我的问题,我遇到此错误:

om.github.tomakehurst.wiremock.common.FatalStartupException:java.lang.RuntimeException:java.net.BindException:地址实用程序     在com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:147)     在fr.pe.rind.service.da058.certification.web.CertificationStepdefs.beforeScenario(CertificationStepdefs.java:52)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处     在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)     在cumul.runtime.Utils $ 1.call(Utils.java:32)     在cumul.runtime.Timeout.timeout(Timeout.java:16)     在cumul.runtime.Utils.invoke(Utils.java:26)     在黄瓜。运行时.java.JavaHookDefinition.execute(JavaHookDefinition.java:60)     在黄瓜。运行时.HookDefinitionMatch.runStep(HookDefinitionMatch.java:17)     在cumul.runner.UnskipableStep.executeStep(UnskipableStep.java:22)     在cumul.api.TestStep.run(TestStep.java:83)     在cumul.api.TestCase.run(TestCase.java:58)     在cumul.runner.Runner.runPickle(Runner.java:80)     在Cucumber.runtime.junit.PickleRunners $ NoStepDescriptions.run(PickleRunners.java:140)     在Cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:68)     在Cucumber.runtime.junit.FeatureRunner.runChild(FeatureRunner.java:23)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)     在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)     在Cucumber.runtime.junit.FeatureRunner.run(FeatureRunner.java:73)     在Cucumber.api.junit.Cucumber.runChild(Cucumber.java:117)     在Cucumber.api.junit.Cucumber.runChild(Cucumber.java:55)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)     在org.junit.runners.ParentRunner上$ 2.evaluate(ParentRunner.java:268)     在cucumber.api.junit.Cucumber $ 1.evaluate(Cucumber.java:126)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)     在org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:365)     在org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:273)     在org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:238)     在org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:159)     在org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:383)     在org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:344)     在org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:125)     在org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:417) 造成原因:java.lang.RuntimeException:java.net.BindException:地址用法     在com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:165)     在com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:145)

2 个答案:

答案 0 :(得分:0)

不可能在同一端口上运行多个应用程序。这不是WireMock的限制,而是一种通用的OS / Application。

答案 1 :(得分:0)

您可以使用动态端口,代码如下

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

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