【问题标题】:Spring webflux WebTestClient with query parameter带有查询参数的 Spring webflux WebTestClient
【发布时间】:2019-10-16 09:10:46
【问题描述】:

在我的 webflux 应用程序中,我有这个 GET 端点

v3/callback?state=cGF5bWVudGlkPTRiMmZlMG

我正在尝试使用WebTestClient 编写集成测试

@Test
public void happyScenario() {
    webTestClient.get().uri("/v3/callback?state=cGF5bWVudGlkPTRiMmZlMG")
            .exchange()
            .expectStatus()
            .isOk();
}

此测试用例返回404 notFound,如果我删除了查询参数,它将被调用,但state 参数将丢失

我尝试使用attribute

  webTestClient.get().uri("/v3/callback")
            .attribute("state","cGF5bWVudGlkPTRiMmZlMG")
            .exchange()
            .expectStatus()
            .isOk();

但仍然缺少state 参数,使用webTestClient 时如何在请求中包含查询参数?

【问题讨论】:

    标签: java testing spring-webflux


    【解决方案1】:

    您可以使用UriBuilder

    webTestClient.get()
                .uri(uriBuilder ->
                        uriBuilder
                                .path("/v3/callback")
                                .queryParam("state", "cGF5bWVudGlkPTRiMmZlMG")
                                .build())
                .exchange()
                .expectStatus()
                .isOk();
    

    这应该可行。

    【讨论】:

      猜你喜欢
      • 2020-10-24
      • 1970-01-01
      • 2018-01-08
      • 2020-02-11
      • 2018-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-09
      相关资源
      最近更新 更多