gpt4 book ai didi

java - Wiremock 单元测试在本地失败 - java.net.BindException : Address already in use: JVM_Bind

转载 作者:行者123 更新时间:2023-11-30 07:06:14 26 4
gpt4 key购买 nike

我在使用 Wiremock 的项目中进行了此测试,它在 Jenkins 中正常工作(在构建期间,测试正在执行并通过)但它在我的本地计算机中失败并出现以下错误(Eclipse 控制台):

14:06:17,692  INFO [org.mortbay.log] - Logging to org.slf4j.impl.Log4jLoggerAdapter(org.mortbay.log) via org.mortbay.log.Slf4jLog
14:06:17,751 INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,767 WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,767 WARN [org.mortbay.log] - failed Server@549b6220: java.net.BindException: Address already in use: JVM_Bind
14:06:17,775 INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,776 WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,776 WARN [org.mortbay.log] - failed Server@1c1474ea: java.net.BindException: Address already in use: JVM_Bind
14:06:17,777 INFO [org.mortbay.log] - jetty-6.1.26
14:06:17,778 WARN [org.mortbay.log] - failed DelayableSocketConnector@0.0.0.0:62344: java.net.BindException: Address already in use: JVM_Bind
14:06:17,778 WARN [org.mortbay.log] - failed Server@34c7e8a7: java.net.BindException: Address already in use: JVM_Bind

(行家)

Tests run: 3, Failures: 0, Errors: 3, Skipped: 0, Time elapsed: 0.408 sec <<< FAILURE!
Time elapsed: 0.377 sec <<< ERROR!
java.lang.RuntimeException: java.net.BindException: Address already in use: JVM_Bind
at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:188)
at com.github.tomakehurst.wiremock.junit.WireMockClassRule$1.evaluate(WireMockClassRule.java:75)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:35)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:115)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at com.sun.proxy.$Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:150)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:91)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:383)
at java.net.ServerSocket.bind(ServerSocket.java:328)
at java.net.ServerSocket.<init>(ServerSocket.java:194)
at org.mortbay.jetty.bio.SocketConnector.newServerSocket(SocketConnector.java:80)
at org.mortbay.jetty.bio.SocketConnector.open(SocketConnector.java:73)
at org.mortbay.jetty.AbstractConnector.doStart(AbstractConnector.java:283)
at org.mortbay.jetty.bio.SocketConnector.doStart(SocketConnector.java:147)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at org.mortbay.jetty.Server.doStart(Server.java:235)
at org.mortbay.component.AbstractLifeCycle.start(AbstractLifeCycle.java:50)
at com.github.tomakehurst.wiremock.WireMockServer.start(WireMockServer.java:186)
... 21 more

我调试了代码以检查我的测试中是否有错误,但我什至无法到达那里,因为它在创建 WireMockClassRule 时失败(请参见下面的代码)。

private static int VVS_PORT_MOCK;
static {
try {
VVS_PORT_MOCK = (new ServerSocket(0)).getLocalPort();
} catch (IOException e) {
//
}
}

@Rule
public static WireMockClassRule wireMockRule = new WireMockClassRule(VVS_PORT_MOCK);

问题似乎出在端口的动态分配上,因为如果我对端口值进行硬编码:

 private static int VVS_PORT_MOCK = 8989; //using this it works

所有测试都顺利通过(但由于这是一个单元测试,我试图避免对其进行硬编码,因为我想确保测试将通过所有地方)。

(之前用netstat查过分配的端口有没有被占用,没问题)

我认为这与我缺少的一些本地配置有关,但此时我迷路了。

对可能发生的事情有什么想法吗?

此外,如果需要更多信息来解决此问题,请告诉我。

谢谢!

最佳答案

当您运行方法“getLocalPort()”并且当代码实例化 WireMockClassRule 时,ServerSocket 保留端口,该端口已经绑定(bind),导致您面临的异常。

您可以像这样更改提供的代码块:

ServerSocket ss = null;
try {
ss = new ServerSocket(0);
PORT = ss.getLocalPort();
} catch (IOException e) {
LOG.error("Socket Exception while opening socket:", e);
} finally {
try {
ss.close();
} catch (IOException e) {
LOG.error("Socket Exception while closing socket:", e);
}
}

如果有用请告诉我

关于java - Wiremock 单元测试在本地失败 - java.net.BindException : Address already in use: JVM_Bind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25857164/

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