【问题标题】:@AutoConfigureWebTestClient(timeout = "600000") has no effect@AutoConfigureWebTestClient(timeout = "600000") 没有效果
【发布时间】:2019-11-09 01:51:22
【问题描述】:

我正在使用WebTestClient 对控制器进行一些集成测试。如果我在控制器内设置断点,我会达到WebTestClient 的 5 秒标准超时。对此的解决方案是将@AutoConfigureWebTestClient(timeout = "600000") 添加到我的测试中,如她所说的Timeout on blocking read for 5000 MILLISECONDS in Spring WEBFLUX

对我来说@AutoConfigureWebTestClient(timeout = "600000") 不会改变任何东西。 5秒后我仍然得到超时异常。

有什么想法吗?

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@ActiveProfiles("test")
@Transactional
@Import(EntityFactoryConfiguration.class)
@AutoConfigureWebTestClient(timeout = "600000") // giv me 10 min for debugging
public class LogControllerIntegrationTest {

    ...

    @Autowired
    private WebTestClient webTestClient;

    ...

    @Test
    public void myTest() {
        ...
        webTestClient.post().uri("/log")
                .contentType(MediaType.APPLICATION_JSON_UTF8)
                .accept(MediaType.APPLICATION_JSON_UTF8)
                .body(BodyInserters.fromObject(protocolLine))
                .exchange()
                .expectStatus().isOk();

    }

【问题讨论】:

  • 我认为这个答案帮助你解决了问题,你能接受它来结束这个问题吗?谢谢!

标签: java spring-boot junit spring-boot-test


【解决方案1】:

我认为该注释在您的设置中不起作用,因为默认情况下超时为already 5 seconds,尝试该链接问题的其他答案如何?

@BeforeEach
public void setUp() {
     webTestClient = webTestClient
                        .mutate()
                        .responseTimeout(Duration.ofMillis(600000))
                        .build();
}

您可以从它的名称中看到@AutoConfigureWebTestClient 正在尝试自动配置WebTestClient,但我认为您以某种方式通过自动装配WebTestClient 来覆盖它,也许还配置它?所以继续手动设置超时!

更新

Afaics,只需添加spring-boot-starter-webflux 作为依赖项就足以获得WebTestClient bean,所以我认为甚至不需要@AutoConfigureWebTestClient。检查this。你能确认一下吗?

【讨论】:

  • 好的,改变我的 WebTestClient 实例有效。但是如果我不应该使用@Autowired@AutoConfigureWebTestClient 应该如何工作?
  • @BetaRide 我认为只是自动装配不应该破坏它,您是否在任何其他地方对其进行配置或变异?我更新了@AutoConfigureWebTestClient,你能检查一下吗?
  • 不,我不会改变 WebTestClient 其他任何内容。所以我仍然不明白为什么 @AutoConfigureWebTestClient 不应该与 @Autowired 一起使用。
  • @BetaRide 您是否尝试在不添加@AutoConfigureWebTestClient 的情况下运行测试? WebTestClient 可以自动装配吗?我现在不能测试,对不起。 AutoConfigureWebTestClient 类本身看起来非常有限,也没有很好的文档记录,也许最好不要使用它,如果你可以让它在没有它的情况下工作?
  • 抱歉,如果不清楚!一切都根据您的答案进行。只需改变响应时间,不要使用@AutoConfigureWebTestClient。我只是好奇为什么注释存在。但也许我只是好奇。
【解决方案2】:

我知道这个问题已经过去一年了。 但是到目前为止,这个注释在给定的超时下工作得很好 这是我的配置

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureWebTestClient(timeout = "10000")
@ActiveProfiles("test")
public class SomeIntegrationTests {

    @Autowired
    private WebTestClient webClient;

也许现在已经解决了一个旧错误..

【讨论】:

  • 不,它仍然不起作用。超时是按要求设置的,但阻塞 execute() 的异常仍然会导致 java.lang.IllegalStateException: Timeout on blocking read for 100000 MILLISECONDS...
【解决方案3】:

@AutoConfigureWebTestClient 不接受毫秒值,而是使用

解析成 Duration 的字符串
Duration.parse(CharSequence)

@AutoConfigureWebTestClient 的 Javadoc 中所述

客户端的超时时间(以任何格式处理 Duration.parse(CharSequence)).

持续时间字符串的格式在Duration Javadoc进行了解释

要将超时设置为 10 分钟,您应该指定以下内容:

@AutoConfigureWebTestClient(timeout = "PT10M")

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 2012-10-30
    • 2018-05-21
    • 2018-07-28
    • 2018-05-15
    • 1970-01-01
    • 2011-10-01
    • 2015-05-31
    相关资源
    最近更新 更多