首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有MockWebServer的WebTestClient挂起

带有MockWebServer的WebTestClient挂起
EN

Stack Overflow用户
提问于 2020-06-13 01:36:44
回答 1查看 698关注 0票数 1

我有一个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结束。

代码语言:javascript
复制
@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();
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2020-06-15 04:09:16

您确定您的集成测试使用您在@TestConfiguration中指定的WebClient bean吗?

似乎缺少一个@Bean注释:

代码语言:javascript
复制
@TestConfiguration
static class SpringConfiguration {

    @Bean
    WebClient remoteClient() {
        return WebClient.builder()
                        .baseUrl("http://" + remoteServer.getHostName() + ":" + remoteServer.getPort())
                        .build();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62349566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档