【问题标题】:Connection refused in integration test after migration from `@WebFluxTest` to `@SpringBootTest`从“@WebFluxTest”迁移到“@SpringBootTest”后,集成测试中的连接被拒绝
【发布时间】:2020-04-16 14:10:31
【问题描述】:

我从 Spring Boot 响应式迁移到 mvc。我迁移了控制器,现在我尝试迁移集成测试。

控制器的测试是这样注释的,如果我运行测试它就可以工作。

@RunWith(SpringRunner.class)
@WebFluxTest
public class MyIntegrationTest {
}

然后我像这样替换WebFluxTest注解

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyIntegrationTest {
}

如果我运行这个测试,我有reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8080。任何想法如何解决它?

【问题讨论】:

    标签: java spring-boot spring-mvc spring-webflux webtestclient


    【解决方案1】:
    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
    

    将在随机端口上加载并启动您的完整应用程序。

    @AutoConfigureWebTestClient
    

    将加载您的应用程序的模拟,然后配置 WebTestClient 以转到该模拟。

    您是在告诉 Spring 启动应用程序并加载模拟。

    所有这些在文档中都有很好的解释,所以请阅读那里然后决定是否要加载整个应用程序,或者只是在执行测试时模拟它。

    Testing Spring boot applications

    Tests with a running server

    Tests with a mock environment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-19
      • 2016-09-24
      • 2020-04-10
      • 2012-12-19
      • 2021-03-16
      • 2019-05-30
      • 1970-01-01
      • 2017-08-08
      相关资源
      最近更新 更多