mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
spring webflux 示例
This commit is contained in:
@@ -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<UserVO> 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<UserVO> 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<Integer> add(@RequestBody Publisher<UserAddDTO> addDTO) {
|
||||
// 插入用户记录,返回编号
|
||||
Integer returnId = UUID.randomUUID().hashCode();
|
||||
Integer returnId = 1;
|
||||
// 返回用户编号
|
||||
return Mono.just(returnId);
|
||||
}
|
||||
@@ -72,7 +90,7 @@ public class UserController {
|
||||
@PostMapping("add2")
|
||||
public Mono<Integer> add2(Mono<UserAddDTO> 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<Boolean> delete(@RequestParam("id") Integer id) {
|
||||
// 删除用户记录
|
||||
Boolean success = false;
|
||||
Boolean success = true;
|
||||
// 返回是否更新成功
|
||||
return Mono.just(success);
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<String, Object> 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<String> 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<String, Object> 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<EntityExchangeResult<Boolean>>) 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<EntityExchangeResult<Boolean>>) result -> // 通过消费结果,判断符合是 true 。
|
||||
Assert.assertTrue("返回结果需要为 true", result.getResponseBody()));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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" +
|
||||
"}"); // 响应结果
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package cn.iocoder.springboot.lab27.springwebflux;
|
||||
Reference in New Issue
Block a user