从“@WebFluxTest”迁移到“@SpringBootTest”后,集成测试中连接被拒绝

Vit*_*lii 2 java spring-mvc spring-boot spring-webflux webtestclient

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

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

@RunWith(SpringRunner.class)
@WebFluxTest
public class MyIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

然后我WebFluxTest像这样替换注释

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyIntegrationTest {
}
Run Code Online (Sandbox Code Playgroud)

如果我运行这个测试,我就有了reactor.core.Exceptions$ReactiveException: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: localhost/127.0.0.1:8080。有什么想法如何修复它吗?

Tho*_*olf 5

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
Run Code Online (Sandbox Code Playgroud)

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

@AutoConfigureWebTestClient
Run Code Online (Sandbox Code Playgroud)

将加载应用程序的模拟,然后配置 WebTestClient 来访问该模拟。

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

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

测试 Spring boot 应用程序

使用正在运行的服务器进行测试

使用模拟环境进行测试