繁体   English   中英

如何使用 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();

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM