Wiremock单元测试在本地失败 - java.net.BindException:已在使用的地址:JVM_Bind

时间:2014-09-15 21:27:29

标签: java eclipse junit jenkins wiremock

我的项目中有这个测试,它使用的是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

(Maven的)

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以查看之前是否使用了分配的端口,但那里没有问题)

我认为这与我失踪的一些本地配置有关,但此时我已经迷失了。

关于可能发生的事情的任何想法?

另外,如果需要进一步的信息来解决这个问题,请告诉我。

Thnaks!

1 个答案:

答案 0 :(得分:4)

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

您可以更改以下内容提供的代码块:

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);
    }
}

让我知道它是否有效