Skip to content

Commit fe88a20

Browse files
author
YunaiV
committed
spring webflux 示例
1 parent 04efb72 commit fe88a20

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.junit.Test;
66
import org.junit.runner.RunWith;
77
import org.springframework.beans.factory.annotation.Autowired;
8-
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux;
98
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
109
import org.springframework.boot.test.context.SpringBootTest;
1110
import org.springframework.test.context.junit4.SpringRunner;
@@ -22,7 +21,6 @@
2221
*/
2322
@RunWith(SpringRunner.class)
2423
@SpringBootTest(classes = Application.class)
25-
@AutoConfigureWebFlux
2624
@AutoConfigureWebTestClient
2725
public class UserControllerTest {
2826

@@ -51,7 +49,7 @@ public void testList() {
5149
}
5250

5351
@Test
54-
public void testGet() throws Exception {
52+
public void testGet() {
5553
// 获得指定用户编号的用户
5654
webClient.get().uri("/users/get?id=1")
5755
.exchange() // 执行请求
@@ -63,7 +61,7 @@ public void testGet() throws Exception {
6361
}
6462

6563
@Test
66-
public void testGet2() throws Exception {
64+
public void testGet2() {
6765
// 获得指定用户编号的用户
6866
webClient.get().uri("/users/v2/get?id=1")
6967
.exchange() // 执行请求
@@ -75,7 +73,7 @@ public void testGet2() throws Exception {
7573
}
7674

7775
@Test
78-
public void testAdd() throws Exception {
76+
public void testAdd() {
7977
Map<String, Object> params = new HashMap<>();
8078
params.put("username", "yudaoyuanma");
8179
params.put("password", "nicai");
@@ -88,7 +86,7 @@ public void testAdd() throws Exception {
8886
}
8987

9088
@Test
91-
public void testAdd2() throws Exception {
89+
public void testAdd2() { // 发送文件的测试,可以参考 https://dev.to/shavz/sending-multipart-form-data-using-spring-webtestclient-2gb7 文章
9290
BodyInserters.FormInserter<String> formData = // Form Data 数据,需要这么拼凑
9391
BodyInserters.fromFormData("username", "yudaoyuanma")
9492
.with("password", "nicai");
@@ -102,7 +100,7 @@ public void testAdd2() throws Exception {
102100

103101

104102
@Test
105-
public void testUpdate() throws Exception {
103+
public void testUpdate() {
106104
Map<String, Object> params = new HashMap<>();
107105
params.put("id", 1);
108106
params.put("username", "yudaoyuanma");
@@ -117,14 +115,15 @@ public void testUpdate() throws Exception {
117115
}
118116

119117
@Test
120-
public void testDelete() throws Exception {
118+
public void testDelete() {
121119
// 删除用户
122120
webClient.post().uri("/users/delete?id=1")
123121
.exchange() // 执行请求
124122
.expectStatus().isOk() // 响应状态码 200
125123
.expectBody(Boolean.class) // 期望返回值类型是 Boolean
126-
.consumeWith((Consumer<EntityExchangeResult<Boolean>>) result -> // 通过消费结果,判断符合是 true 。
127-
Assert.assertTrue("返回结果需要为 true", result.getResponseBody()));
124+
.isEqualTo(true); // 这样更加简洁一些
125+
// .consumeWith((Consumer<EntityExchangeResult<Boolean>>) result -> // 通过消费结果,判断符合是 true 。
126+
// Assert.assertTrue("返回结果需要为 true", result.getResponseBody()));
128127
}
129128

130129
}

0 commit comments

Comments
 (0)