From 04efb720adbc825e2c6ffd309f3ccfd69fa6a9af Mon Sep 17 00:00:00 2001 From: YunaiV <> Date: Sun, 24 Nov 2019 23:42:24 +0800 Subject: [PATCH] =?UTF-8?q?spring=20webflux=20=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserController.java | 28 +++- .../springwebflux/service/UserService.java | 13 ++ .../controller/UserControllerTest.java | 130 ++++++++++++++++++ .../controller/UserControllerTest2.java | 47 +++++++ .../lab27/springwebflux/package-info.java | 1 + 5 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/service/UserService.java create mode 100644 lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest.java create mode 100644 lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest2.java create mode 100644 lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/package-info.java diff --git a/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java b/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java index 6ec06f4d..be16341d 100644 --- a/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java +++ b/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserController.java @@ -2,15 +2,16 @@ package cn.iocoder.springboot.lab27.springwebflux.controller; import cn.iocoder.springboot.lab27.springwebflux.dto.UserAddDTO; import cn.iocoder.springboot.lab27.springwebflux.dto.UserUpdateDTO; +import cn.iocoder.springboot.lab27.springwebflux.service.UserService; import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO; import org.reactivestreams.Publisher; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import java.util.ArrayList; import java.util.List; -import java.util.UUID; /** * 用户 Controller @@ -19,6 +20,9 @@ import java.util.UUID; @RequestMapping("/users") public class UserController { + @Autowired + private UserService userService; + /** * 查询用户列表 * @@ -44,7 +48,21 @@ public class UserController { @GetMapping("/get") public Mono get(@RequestParam("id") Integer id) { // 查询用户 - UserVO user = new UserVO().setId(id).setUsername(UUID.randomUUID().toString()); + UserVO user = new UserVO().setId(id).setUsername("username:" + id); + // 返回 + return Mono.just(user); + } + + /** + * 获得指定用户编号的用户 + * + * @param id 用户编号 + * @return 用户 + */ + @GetMapping("/v2/get") + public Mono get2(@RequestParam("id") Integer id) { + // 查询用户 + UserVO user = userService.get(id); // 返回 return Mono.just(user); } @@ -58,7 +76,7 @@ public class UserController { @PostMapping("add") public Mono add(@RequestBody Publisher addDTO) { // 插入用户记录,返回编号 - Integer returnId = UUID.randomUUID().hashCode(); + Integer returnId = 1; // 返回用户编号 return Mono.just(returnId); } @@ -72,7 +90,7 @@ public class UserController { @PostMapping("add2") public Mono add2(Mono addDTO) { // 插入用户记录,返回编号 - Integer returnId = UUID.randomUUID().hashCode(); + Integer returnId = 1; // 返回用户编号 return Mono.just(returnId); } @@ -100,7 +118,7 @@ public class UserController { @PostMapping("/delete") // URL 修改成 /delete ,RequestMethod 改成 DELETE public Mono delete(@RequestParam("id") Integer id) { // 删除用户记录 - Boolean success = false; + Boolean success = true; // 返回是否更新成功 return Mono.just(success); } diff --git a/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/service/UserService.java b/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/service/UserService.java new file mode 100644 index 00000000..00332350 --- /dev/null +++ b/lab-27/lab-27-webflux-01/src/main/java/cn/iocoder/springboot/lab27/springwebflux/service/UserService.java @@ -0,0 +1,13 @@ +package cn.iocoder.springboot.lab27.springwebflux.service; + +import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO; +import org.springframework.stereotype.Service; + +@Service +public class UserService { + + public UserVO get(Integer id) { + return new UserVO().setId(id).setUsername("test"); + } + +} diff --git a/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest.java b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest.java new file mode 100644 index 00000000..349f0d3b --- /dev/null +++ b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest.java @@ -0,0 +1,130 @@ +package cn.iocoder.springboot.lab27.springwebflux.controller; + +import cn.iocoder.springboot.lab27.springwebflux.Application; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.EntityExchangeResult; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.BodyInserters; + +import java.util.HashMap; +import java.util.Map; +import java.util.function.Consumer; + +/** + * UserController 集成测试 + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +@AutoConfigureWebFlux +@AutoConfigureWebTestClient +public class UserControllerTest { + + @Autowired + private WebTestClient webClient; + + @Test + public void testList() { + webClient.get().uri("/users/list") + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("[\n" + + " {\n" + + " \"id\": 1,\n" + + " \"username\": \"yudaoyuanma\"\n" + + " },\n" + + " {\n" + + " \"id\": 2,\n" + + " \"username\": \"woshiyutou\"\n" + + " },\n" + + " {\n" + + " \"id\": 3,\n" + + " \"username\": \"chifanshuijiao\"\n" + + " }\n" + + "]"); // 响应结果 + } + + @Test + public void testGet() throws Exception { + // 获得指定用户编号的用户 + webClient.get().uri("/users/get?id=1") + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("{\n" + + " \"id\": 1,\n" + + " \"username\": \"username:1\"\n" + + "}"); // 响应结果 + } + + @Test + public void testGet2() throws Exception { + // 获得指定用户编号的用户 + webClient.get().uri("/users/v2/get?id=1") + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("{\n" + + " \"id\": 1,\n" + + " \"username\": \"test\"\n" + + "}"); // 响应结果 + } + + @Test + public void testAdd() throws Exception { + Map params = new HashMap<>(); + params.put("username", "yudaoyuanma"); + params.put("password", "nicai"); + // 添加用户 + webClient.post().uri("/users/add") + .bodyValue(params) + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("1"); // 响应结果。因为没有提供 content 的比较,所以只好使用 json 来比较。竟然能通过 + } + + @Test + public void testAdd2() throws Exception { + BodyInserters.FormInserter formData = // Form Data 数据,需要这么拼凑 + BodyInserters.fromFormData("username", "yudaoyuanma") + .with("password", "nicai"); + // 添加用户 + webClient.post().uri("/users/add2") + .body(formData) + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("1"); // 响应结果。因为没有提供 content 的比较,所以只好使用 json 来比较。竟然能通过 + } + + + @Test + public void testUpdate() throws Exception { + Map params = new HashMap<>(); + params.put("id", 1); + params.put("username", "yudaoyuanma"); + // 修改用户 + webClient.post().uri("/users/update") + .bodyValue(params) + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody(Boolean.class) // 期望返回值类型是 Boolean + .consumeWith((Consumer>) result -> // 通过消费结果,判断符合是 true 。 + Assert.assertTrue("返回结果需要为 true", result.getResponseBody())); + } + + @Test + public void testDelete() throws Exception { + // 删除用户 + webClient.post().uri("/users/delete?id=1") + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody(Boolean.class) // 期望返回值类型是 Boolean + .consumeWith((Consumer>) result -> // 通过消费结果,判断符合是 true 。 + Assert.assertTrue("返回结果需要为 true", result.getResponseBody())); + } + +} diff --git a/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest2.java b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest2.java new file mode 100644 index 00000000..beff9786 --- /dev/null +++ b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/controller/UserControllerTest2.java @@ -0,0 +1,47 @@ +package cn.iocoder.springboot.lab27.springwebflux.controller; + +import cn.iocoder.springboot.lab27.springwebflux.service.UserService; +import cn.iocoder.springboot.lab27.springwebflux.vo.UserVO; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.reactive.server.WebTestClient; + +/** + * UserController 单元测试 + * + * 参考 https://howtodoinjava.com/spring-webflux/webfluxtest-with-webtestclient/ 文章 + */ +@RunWith(SpringRunner.class) +@WebFluxTest(UserController.class) +public class UserControllerTest2 { + + @Autowired + private WebTestClient webClient; + + @MockBean + private UserService userService; + + @Test + public void testGet2() throws Exception { + // Mock UserService 的 get 方法 + System.out.println("before mock:" + userService.get(1)); + Mockito.when(userService.get(1)).thenReturn( + new UserVO().setId(1).setUsername("username:1")); + System.out.println("after mock:" + userService.get(1)); + + // 查询用户列表 + webClient.get().uri("/users/v2/get?id=1") + .exchange() // 执行请求 + .expectStatus().isOk() // 响应状态码 200 + .expectBody().json("{\n" + + " \"id\": 1,\n" + + " \"username\": \"username:1\"\n" + + "}"); // 响应结果 + } + +} diff --git a/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/package-info.java b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/package-info.java new file mode 100644 index 00000000..8d8c1a83 --- /dev/null +++ b/lab-27/lab-27-webflux-01/src/test/java/cn/iocoder/springboot/lab27/springwebflux/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.springboot.lab27.springwebflux;