You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何延长Spring GraphQlTester中WebTestClient的默认超时时间?

解决WebTestClient超时问题的方法

你的问题核心是WebTestClient默认5秒的响应超时,无法覆盖大XML文件存储的耗时,超时后请求中断引发后续数据库连接错误。以下是两种延长超时的有效方法:

方法一:自定义GraphQlTester时配置WebTestClient超时

在测试类中手动创建WebTestClient并设置响应超时,再基于它构建GraphQlTester:

@Autowired
private ApplicationContext applicationContext;

private GraphQlTester graphQlTester;

@BeforeEach
void setUp() {
    WebTestClient webTestClient = WebTestClient.bindToApplicationContext(applicationContext)
            .configureClient()
            .responseTimeout(Duration.ofSeconds(30)) // 根据实际业务调整超时时间
            .build();

    this.graphQlTester = GraphQlTester.create(webTestClient);
}

方法二:通过配置类定制WebTestClient.Builder

如果测试类使用@AutoConfigureGraphQlTester注解,可以添加内部配置类来全局修改WebTestClient的超时:

@SpringBootTest
@AutoConfigureGraphQlTester
class YourTestClass {

    @Configuration
    static class TestWebClientConfig {
        @Bean
        WebTestClient.Builder webTestClientBuilder() {
            return WebTestClient.builder()
                    .responseTimeout(Duration.ofMinutes(1)); // 设置1分钟超时,按需调整
        }
    }

    // 测试方法...
}

额外注意事项

  • 超时时间的设置要匹配实际业务的最大耗时,避免过短或过长。
  • 后续的数据库连接错误是超时导致请求中断的连锁反应,解决WebTestClient超时后,这类错误通常会消失。如果仍出现,需要检查数据库连接池的配置(比如Hikari的connectionTimeout、socketTimeout参数),确保数据库端的超时设置足够覆盖操作耗时。

内容的提问来源于stack exchange,提问作者user3300534

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 11:53:23