【问题标题】:JUnit test case error: "Address already in use: bind"JUnit 测试用例错误:“地址已在使用中:绑定”
【发布时间】:2018-02-21 05:29:10
【问题描述】:

我的项目有一个 JUnit 测试用例,它是使用 SpringBoot (1.3.5) & JDK 8. 当我在 STS 中将我的项目作为 JUnit 测试运行时,所有 测试用例通过,但在每个测试用例结束时给出错误,这 是:我正在尝试运行 JUnit Coverage:

ERROR [ main] o.a.coyote.http11.Http11NioProtocol o.a.j.l.DirectJDKLog.log(DirectJDKLog.java:182) - |||||||||Failed to start end point associated with ProtocolHandler ["http-nio-8080"]
java.net.BindException: Address already in use: bind
    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 org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:340)
    at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:773)
    at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:473)
    at org.apache.catalina.connector.Connector.startInternal(Connector.java:986)
    ........
    ........
    ........
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
ERROR [           main] o.apache.catalina.core.StandardService   o.a.j.l.DirectJDKLog.log(DirectJDKLog.java:182) - |||||||||Failed to start connector [Connector[HTTP/1.1-8080]]
org.apache.catalina.LifecycleException: Failed to start component [Connector[HTTP/1.1-8080]]

此错误显示为端口 8080 已在使用中。如何避免这种情况 错误并让我的所有 20 个测试用例在没有这个的情况下在一个流程中运行 错误?

我的代码是:--->

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(KYCNotificationApplication.class)
@ActiveProfiles("local")
@WebIntegrationTest
@IntegrationTest({"server.port=0"})
public class DetermineKYCNotificationServiceImplTest {

    @InjectMocks
    DetermineKYCNotificationServiceImpl kycService;

    @Mock
    KYCOperationsDao kycOperationsDao;

    @Mock
    HttpServletRequest httpServletRequest;

    @Before
    public void setup() throws Exception {
        MockitoAnnotations.initMocks(this);
         ...
    }

    @Test
    public void testMeth1() {
        try {
             .....
            assertTrue(......);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testMeth2() {
        try {
             .....
            assertTrue(......);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

...2o 多个测试用例....

【问题讨论】:

    标签: spring tomcat spring-boot junit


    【解决方案1】:

    在 Spring Boot @IntegrationTest({"server.port=0"}) 注释您的测试类

    在 Spring Boot >= 1.4 中,您可以使用 @SpringBootTest(classes = Application.class , webEnvironment=WebEnvironment.RANDOM_PORT)

    这两种方法具有相同的效果,即; Spring 会分配一个随机端口(并且会神奇地更新测试上下文中的应用程序属性以反映这个特定于测试的端口)。

    更多详情in the docs ...

    如果您需要启动一个完整运行的服务器进行测试,我们建议您使用随机端口。如果您使用@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT),则每次运行测试时都会随机选择一个可用端口。

    @LocalServerPort 注释可用于将实际使用的端口注入到您的测试中。为方便起见,需要对启动的服务器进行 REST 调用的测试可以另外 @AutowireTestRestTemplate 解析到正在运行的服务器的相关链接。

    更新 1:根据您更新的问题,我现在可以看到您正在使用 @WebIntegrationTest。在这种情况下,您可以简单地将 randomPort 参数添加到该注释中,例如

    @WebIntegrationTest(randomPort = true)
    

    【讨论】:

    • 在 STS 中运行 JUnit 代码覆盖时,我仍然面临相同端口正在使用的问题。请提出建议。
    • 在您的测试用例中,您使用 both @WebIntegrationTest@IntegrationTest。这些注释不打算一起使用。通常,您会使用@IntegrationTest({"server.port=0"}) @WebIntegrationTest(randomPort = true) 而不是在不完全理解它们的作用的情况下添加注释 - 也许 - 我强烈建议阅读我在回答中链接的文档并了解什么Spring就是在这种情况下做的。
    • 您好故障-感谢您的及时回复,您的以下建议解决了问题:@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(KYCNotificationApplication.class) @ActiveProfiles("local") @WebIntegrationTest( randomPort = true)
    猜你喜欢
    • 2011-01-09
    • 2015-07-26
    • 2016-10-24
    • 2012-05-02
    • 2020-10-06
    • 1970-01-01
    相关资源
    最近更新 更多