1

我从 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. 任何想法如何解决它?

4

1 回答 1

2
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

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

@AutoConfigureWebTestClient

将加载你的应用程序的模拟,然后配置一个 WebTestClient 去那个模拟。

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

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

测试 Spring Boot 应用程序

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

使用模拟环境进行测试

于 2019-12-26T11:15:29.997 回答