首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用WebTestClient连接http服务器?

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

Stack Overflow用户
提问于 2021-07-20 23:15:18
回答 1查看 41关注 0票数 0

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

代码语言:javascript
复制
@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服务器?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-20 23:15:18

下面的代码可以工作:

代码语言:javascript
复制
WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();

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

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68457359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档