【问题标题】:How to use WebTestClient to connect a http server?如何使用 WebTestClient 连接 http 服务器?
【发布时间】:2021-07-20 15:15:17
【问题描述】:

如 WebTestClient 的 javadoc 所述:This client can connect to any server over HTTP。 但是下面的代码并没有真正通过 http 请求:

@SpringBootTest(webEnvironment = DEFINED_PORT)
@AutoConfigureWebTestClient
public class HelloControllerTest {
    @Autowired
    WebTestClient webTestClient;

    @Test
    public void test_hello() {
        webTestClient
            .get()
            .uri("http://localhost:8080/hello/World")
            .exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$.name").isEqualTo("aaa");
    }

    @Test
    public void test_hello2() {
        webTestClient = webTestClient.mutate().baseUrl("http://localhost:8080").build();// even this does not work
        webTestClient.get()
            .uri("/hello/World")
            .exchange()
            .expectStatus().isOk()
            .expectBody()
            .jsonPath("$.name").isEqualTo("aaa");
    }
}

请帮助如何使用WebTestClient连接http服务器?

【问题讨论】:

    标签: spring spring-webflux


    【解决方案1】:

    此代码有效:

    WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();
    

    感谢您的回答:https://stackoverflow.com/a/63094850/13789176

    【讨论】:

      猜你喜欢
      • 2017-05-26
      • 2011-10-31
      • 1970-01-01
      • 1970-01-01
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      相关资源
      最近更新 更多