我有一个使用“spring-boot-starter-webflux”公开rest api的spring boot应用程序。当我的端点之一被调用时,我的应用程序会调用其他休息服务。
我正在尝试使用 WebTestClient 来模拟调用我的客户端和 MockWebServer 来模拟我调用的外部休息服务进行集成测试。
我还覆盖了一个 spring 配置以使用一个“指向”我的应用程序中的 MockWebServer 的 web 客户端。
我看到,当我启动测试时,我的服务器启动,并且 MockWebServer 启动,但是当我调用我的端点时,MockWebServer 似乎“卡住”并且没有响应。
此外,测试"java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS
以 WebTestClient 结束。
@SpringBootTest(webEnvironment = RANDOM_PORT,
properties = { "spring.main.allow-bean-definition-overriding=true" })
class ApplicationIT {
static MockWebServer remoteServer = new MockWebServer();
@BeforeAll
static void setUp() throws Throwable {
remoteServer.start();
}
@AfterAll
static void tearDown() throws Throwable {
remoteServer.shutdown();
}
@Test
void test(@Autowired WebTestClient client) {
remoteServer.enqueue(new MockResponse().setBody("..."));
client.get()
.uri("...")
.exchange()
.expectStatus()
.isOk();
}
@TestConfiguration
static class SpringConfiguration {
WebClient remoteClient() {
return WebClient.builder()
.baseUrl("http://" + remoteServer.getHostName() + ":" + remoteServer.getPort())
.build();
}
}
}