gpt4 book ai didi

java - Jenkins Junit 中的 WireMock - BindException : Address already in use

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:24:05 25 4
gpt4 key购买 nike

我有一些使用 WireMock 的 Junit 测试。在本地测试通过,但在 Jenkins 上我得到一个错误java.lang.RuntimeException: java.net.BindException: 地址已被使用

在 WireMock 配置中,我设置了 dynamicHttpsPort()(根据文档“在启动时随机分配 HTTPS 端口”)及其在本地工作,但在 Jenkins 上它失败。

我的 WireMock 配置是

    @Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig()
.dynamicHttpsPort()
.keystorePath(certsDir.resolve("server.jks").toString())
.keystorePassword(MY_PASS)
.keystoreType("JKS")
);

我有 2 个 JUnit 类,我在其中创建了一个新的 WireMockRule,它们可以同时运行。在其中一个类(class)中,我有 4 个测试,在另一个类(class)中,我有一个测试。 5 个测试全部失败。

完整的堆栈跟踪是:

com.github.tomakehurst.wiremock.common.FatalStartupException: java.lang.RuntimeException: java.net.BindException: Address already in use
at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:146)
at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:68)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70)
Caused by: java.lang.RuntimeException: java.net.BindException: Address already in use
at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:139)
at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:144)
... 23 more
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at wiremock.org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:321)
at wiremock.org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at wiremock.org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at wiremock.org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at wiremock.org.eclipse.jetty.server.Server.doStart(Server.java:366)
at wiremock.org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at com.github.tomakehurst.wiremock.jetty9.JettyHttpServer.start(JettyHttpServer.java:137)
... 24 more

最佳答案

找到解决方案 here :

The problem is that you're not setting the HTTP port on either, which will always be active and default to 8080 (hence them clashing). If you add dynamicPort() to both configs it should fix it.

因此添加 dynamicPort() 修复了它:

    @Rule
public WireMockRule wireMockRule = new WireMockRule(wireMockConfig()
.dynamicPort()
.dynamicHttpsPort()
.keystorePath(certsDir.resolve("server.jks").toString())
.keystorePassword(MY_PASS)
.keystoreType("JKS")
);

关于java - Jenkins Junit 中的 WireMock - BindException : Address already in use,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46756846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com