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

How to use WebTestClient to connect a http server?

正如 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服务器?

此代码有效:

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

感谢您的回答: