Skip to content

Commit 04efb72

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

File tree

5 files changed

+214
-5
lines changed

5 files changed

+214
-5
lines changed

lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO;
44
import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO;
5+
import cn.iocoder.springboot.lab27.springwebflux.service.UserService;
56
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
67
import org.reactivestreams.Publisher;
8+
import org.springframework.beans.factory.annotation.Autowired;
79
import org.springframework.web.bind.annotation.*;
810
import reactor.core.publisher.Flux;
911
import reactor.core.publisher.Mono;
1012

1113
import java.util.ArrayList;
1214
import java.util.List;
13-
import java.util.UUID;
1415

1516
/**
1617
* 用户 Controller
@@ -19,6 +20,9 @@
1920
@RequestMapping("/users")
2021
public class UserController {
2122

23+
@Autowired
24+
private UserService userService;
25+
2226
/**
2327
* 查询用户列表
2428
*
@@ -44,7 +48,21 @@ public Flux<UserVO> list() {
4448
@GetMapping("/get")
4549
public Mono<UserVO> get(@RequestParam("id") Integer id) {
4650
// 查询用户
47-
UserVO user = new UserVO().setId(id).setUsername(UUID.randomUUID().toString());
51+
UserVO user = new UserVO().setId(id).setUsername("username:" + id);
52+
// 返回
53+
return Mono.just(user);
54+
}
55+
56+
/**
57+
* 获得指定用户编号的用户
58+
*
59+
* @param id 用户编号
60+
* @return 用户
61+
*/
62+
@GetMapping("/v2/get")
63+
public Mono<UserVO> get2(@RequestParam("id") Integer id) {
64+
// 查询用户
65+
UserVO user = userService.get(id);
4866
// 返回
4967
return Mono.just(user);
5068
}
@@ -58,7 +76,7 @@ public Mono<UserVO> get(@RequestParam("id") Integer id) {
5876
@PostMapping("add")
5977
public Mono<Integer> add(@RequestBody Publisher<UserAddDTO> addDTO) {
6078
// 插入用户记录,返回编号
61-
Integer returnId = UUID.randomUUID().hashCode();
79+
Integer returnId = 1;
6280
// 返回用户编号
6381
return Mono.just(returnId);
6482
}
@@ -72,7 +90,7 @@ public Mono<Integer> add(@RequestBody Publisher<UserAddDTO> addDTO) {
7290
@PostMapping("add2")
7391
public Mono<Integer> add2(Mono<UserAddDTO> addDTO) {
7492
// 插入用户记录,返回编号
75-
Integer returnId = UUID.randomUUID().hashCode();
93+
Integer returnId = 1;
7694
// 返回用户编号
7795
return Mono.just(returnId);
7896
}
@@ -100,7 +118,7 @@ public Mono<Boolean> update(@RequestBody Publisher<UserUpdateDTO> updateDTO) {
100118
@PostMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE
101119
public Mono<Boolean> delete(@RequestParam("id") Integer id) {
102120
// 删除用户记录
103-
Boolean success = false;
121+
Boolean success = true;
104122
// 返回是否更新成功
105123
return Mono.just(success);
106124
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.service;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
public class UserService {
8+
9+
public UserVO get(Integer id) {
10+
return new UserVO().setId(id).setUsername("test");
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.controller;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.Application;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux;
9+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.reactive.server.EntityExchangeResult;
13+
import org.springframework.test.web.reactive.server.WebTestClient;
14+
import org.springframework.web.reactive.function.BodyInserters;
15+
16+
import java.util.HashMap;
17+
import java.util.Map;
18+
import java.util.function.Consumer;
19+
20+
/**
21+
* UserController 集成测试
22+
*/
23+
@RunWith(SpringRunner.class)
24+
@SpringBootTest(classes = Application.class)
25+
@AutoConfigureWebFlux
26+
@AutoConfigureWebTestClient
27+
public class UserControllerTest {
28+
29+
@Autowired
30+
private WebTestClient webClient;
31+
32+
@Test
33+
public void testList() {
34+
webClient.get().uri("/users/list")
35+
.exchange() // 执行请求
36+
.expectStatus().isOk() // 响应状态码 200
37+
.expectBody().json("[\n" +
38+
" {\n" +
39+
" \"id\": 1,\n" +
40+
" \"username\": \"yudaoyuanma\"\n" +
41+
" },\n" +
42+
" {\n" +
43+
" \"id\": 2,\n" +
44+
" \"username\": \"woshiyutou\"\n" +
45+
" },\n" +
46+
" {\n" +
47+
" \"id\": 3,\n" +
48+
" \"username\": \"chifanshuijiao\"\n" +
49+
" }\n" +
50+
"]"); // 响应结果
51+
}
52+
53+
@Test
54+
public void testGet() throws Exception {
55+
// 获得指定用户编号的用户
56+
webClient.get().uri("/users/get?id=1")
57+
.exchange() // 执行请求
58+
.expectStatus().isOk() // 响应状态码 200
59+
.expectBody().json("{\n" +
60+
" \"id\": 1,\n" +
61+
" \"username\": \"username:1\"\n" +
62+
"}"); // 响应结果
63+
}
64+
65+
@Test
66+
public void testGet2() throws Exception {
67+
// 获得指定用户编号的用户
68+
webClient.get().uri("/users/v2/get?id=1")
69+
.exchange() // 执行请求
70+
.expectStatus().isOk() // 响应状态码 200
71+
.expectBody().json("{\n" +
72+
" \"id\": 1,\n" +
73+
" \"username\": \"test\"\n" +
74+
"}"); // 响应结果
75+
}
76+
77+
@Test
78+
public void testAdd() throws Exception {
79+
Map<String, Object> params = new HashMap<>();
80+
params.put("username", "yudaoyuanma");
81+
params.put("password", "nicai");
82+
// 添加用户
83+
webClient.post().uri("/users/add")
84+
.bodyValue(params)
85+
.exchange() // 执行请求
86+
.expectStatus().isOk() // 响应状态码 200
87+
.expectBody().json("1"); // 响应结果。因为没有提供 content 的比较,所以只好使用 json 来比较。竟然能通过
88+
}
89+
90+
@Test
91+
public void testAdd2() throws Exception {
92+
BodyInserters.FormInserter<String> formData = // Form Data 数据,需要这么拼凑
93+
BodyInserters.fromFormData("username", "yudaoyuanma")
94+
.with("password", "nicai");
95+
// 添加用户
96+
webClient.post().uri("/users/add2")
97+
.body(formData)
98+
.exchange() // 执行请求
99+
.expectStatus().isOk() // 响应状态码 200
100+
.expectBody().json("1"); // 响应结果。因为没有提供 content 的比较,所以只好使用 json 来比较。竟然能通过
101+
}
102+
103+
104+
@Test
105+
public void testUpdate() throws Exception {
106+
Map<String, Object> params = new HashMap<>();
107+
params.put("id", 1);
108+
params.put("username", "yudaoyuanma");
109+
// 修改用户
110+
webClient.post().uri("/users/update")
111+
.bodyValue(params)
112+
.exchange() // 执行请求
113+
.expectStatus().isOk() // 响应状态码 200
114+
.expectBody(Boolean.class) // 期望返回值类型是 Boolean
115+
.consumeWith((Consumer<EntityExchangeResult<Boolean>>) result -> // 通过消费结果,判断符合是 true 。
116+
Assert.assertTrue("返回结果需要为 true", result.getResponseBody()));
117+
}
118+
119+
@Test
120+
public void testDelete() throws Exception {
121+
// 删除用户
122+
webClient.post().uri("/users/delete?id=1")
123+
.exchange() // 执行请求
124+
.expectStatus().isOk() // 响应状态码 200
125+
.expectBody(Boolean.class) // 期望返回值类型是 Boolean
126+
.consumeWith((Consumer<EntityExchangeResult<Boolean>>) result -> // 通过消费结果,判断符合是 true 。
127+
Assert.assertTrue("返回结果需要为 true", result.getResponseBody()));
128+
}
129+
130+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cn.iocoder.springboot.lab27.springwebflux.controller;
2+
3+
import cn.iocoder.springboot.lab27.springwebflux.service.UserService;
4+
import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.mockito.Mockito;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
10+
import org.springframework.boot.test.mock.mockito.MockBean;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
import org.springframework.test.web.reactive.server.WebTestClient;
13+
14+
/**
15+
* UserController 单元测试
16+
*
17+
* 参考 https://howtodoinjava.com/spring-webflux/webfluxtest-with-webtestclient/ 文章
18+
*/
19+
@RunWith(SpringRunner.class)
20+
@WebFluxTest(UserController.class)
21+
public class UserControllerTest2 {
22+
23+
@Autowired
24+
private WebTestClient webClient;
25+
26+
@MockBean
27+
private UserService userService;
28+
29+
@Test
30+
public void testGet2() throws Exception {
31+
// Mock UserService 的 get 方法
32+
System.out.println("before mock:" + userService.get(1));
33+
Mockito.when(userService.get(1)).thenReturn(
34+
new UserVO().setId(1).setUsername("username:1"));
35+
System.out.println("after mock:" + userService.get(1));
36+
37+
// 查询用户列表
38+
webClient.get().uri("/users/v2/get?id=1")
39+
.exchange() // 执行请求
40+
.expectStatus().isOk() // 响应状态码 200
41+
.expectBody().json("{\n" +
42+
" \"id\": 1,\n" +
43+
" \"username\": \"username:1\"\n" +
44+
"}"); // 响应结果
45+
}
46+
47+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package cn.iocoder.springboot.lab27.springwebflux;

0 commit comments

Comments
 (0)