我有一个spring boot应用程序,它使用"spring-boot-starter-webflux“公开rest api。当我的一个端点被调用时,我的应用程序调用其他rest服务。
我正在尝试使用WebTestClient来模拟调用我的客户机,并使用MockWebServer来模拟我调用的外部rest服务来进行集成测试。
我还重写了spring配置,以使用“指向”我应用程序中的MockWebServer的webclient。
我看到,当我启动测试时,我的服务器启动了,MockWebServer也启动了,但是当我调用我的端点时,MockWebServer似乎“卡住”了,没有响应。
此外,测试以来自WebTestClient的"java.lang.IllegalStateException: Timeout on blocking read for 5000 MILLISECONDS结束。
@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();
}
}
}发布于 2020-06-15 04:09:16
您确定您的集成测试使用您在@TestConfiguration中指定的WebClient bean吗?
似乎缺少一个@Bean注释:
@TestConfiguration
static class SpringConfiguration {
@Bean
WebClient remoteClient() {
return WebClient.builder()
.baseUrl("http://" + remoteServer.getHostName() + ":" + remoteServer.getPort())
.build();
}
}https://stackoverflow.com/questions/62349566
复制相似问题