5
5
import org .junit .Test ;
6
6
import org .junit .runner .RunWith ;
7
7
import org .springframework .beans .factory .annotation .Autowired ;
8
- import org .springframework .boot .test .autoconfigure .web .reactive .AutoConfigureWebFlux ;
9
8
import org .springframework .boot .test .autoconfigure .web .reactive .AutoConfigureWebTestClient ;
10
9
import org .springframework .boot .test .context .SpringBootTest ;
11
10
import org .springframework .test .context .junit4 .SpringRunner ;
22
21
*/
23
22
@ RunWith (SpringRunner .class )
24
23
@ SpringBootTest (classes = Application .class )
25
- @ AutoConfigureWebFlux
26
24
@ AutoConfigureWebTestClient
27
25
public class UserControllerTest {
28
26
@@ -51,7 +49,7 @@ public void testList() {
51
49
}
52
50
53
51
@ Test
54
- public void testGet () throws Exception {
52
+ public void testGet () {
55
53
// 获得指定用户编号的用户
56
54
webClient .get ().uri ("/users/get?id=1" )
57
55
.exchange () // 执行请求
@@ -63,7 +61,7 @@ public void testGet() throws Exception {
63
61
}
64
62
65
63
@ Test
66
- public void testGet2 () throws Exception {
64
+ public void testGet2 () {
67
65
// 获得指定用户编号的用户
68
66
webClient .get ().uri ("/users/v2/get?id=1" )
69
67
.exchange () // 执行请求
@@ -75,7 +73,7 @@ public void testGet2() throws Exception {
75
73
}
76
74
77
75
@ Test
78
- public void testAdd () throws Exception {
76
+ public void testAdd () {
79
77
Map <String , Object > params = new HashMap <>();
80
78
params .put ("username" , "yudaoyuanma" );
81
79
params .put ("password" , "nicai" );
@@ -88,7 +86,7 @@ public void testAdd() throws Exception {
88
86
}
89
87
90
88
@ Test
91
- public void testAdd2 () throws Exception {
89
+ public void testAdd2 () { // 发送文件的测试,可以参考 https://dev.to/shavz/sending-multipart-form-data-using-spring-webtestclient-2gb7 文章
92
90
BodyInserters .FormInserter <String > formData = // Form Data 数据,需要这么拼凑
93
91
BodyInserters .fromFormData ("username" , "yudaoyuanma" )
94
92
.with ("password" , "nicai" );
@@ -102,7 +100,7 @@ public void testAdd2() throws Exception {
102
100
103
101
104
102
@ Test
105
- public void testUpdate () throws Exception {
103
+ public void testUpdate () {
106
104
Map <String , Object > params = new HashMap <>();
107
105
params .put ("id" , 1 );
108
106
params .put ("username" , "yudaoyuanma" );
@@ -117,14 +115,15 @@ public void testUpdate() throws Exception {
117
115
}
118
116
119
117
@ Test
120
- public void testDelete () throws Exception {
118
+ public void testDelete () {
121
119
// 删除用户
122
120
webClient .post ().uri ("/users/delete?id=1" )
123
121
.exchange () // 执行请求
124
122
.expectStatus ().isOk () // 响应状态码 200
125
123
.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()));
128
127
}
129
128
130
129
}
0 commit comments