正如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服务器?
发布于 2021-07-20 23:15:18
下面的代码可以工作:
WebTestClient webTestClient = WebTestClient.bindToServer().baseUrl("http://localhost:8080").build();https://stackoverflow.com/questions/68457359
复制相似问题