当前位置:   article > 正文

使用了WireMock的HTTPS端口的Junit测试在Jenkins中报错BindException: Address already in use的解决办法_wiremock 端口被占用

wiremock 端口被占用
问题描述

Junit测试中使用了WireMock测试https端口,在本地所有测试均无异常,但放在Jenkins服务器中运行时报错,测试部分代码如下,使用随机端口:

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

private WireMockServer wm = new WireMockServer(options()
			.dynamicHttpsPort()
            .keystorePath(CERTIFICATE_PATH)
            .keystorePassword(CERTIFICATE_PASSWORD)
            .keystoreType("JKS"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

错误堆栈信息如下:

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
原因分析

WireMockServer在使用https端口的情况下,启动时会同时开启https端口和http端口,在指定https端口的同时未指定http端口,http端口使用默认的8080,Jenkins上该端口被占用。

解决办法

配置https端口的同时配置http端口:

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

private WireMockServer wm = new WireMockServer(options()
			.dynamicPort()
			.dynamicHttpsPort()
            .keystorePath(CERTIFICATE_PATH)
            .keystorePassword(CERTIFICATE_PASSWORD)
            .keystoreType("JKS"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如果使用WireMockRule同理:

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;

@Rule
WireMockRule wm = new WireMockRule(options()
			.dynamicPort()
			.dynamicHttpsPort()
            .keystorePath(CERTIFICATE_PATH)
            .keystorePassword(CERTIFICATE_PASSWORD)
            .keystoreType("JKS"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号