在日常工作中,免不了自测 UT,因为覆盖率不达标,是不允许提交测试,那怎么进行 WebFlux 项目的测试呢。@WebFluxTest 是 WebFlux 测试的重要注解。
新建一个工程


目录核心如下:
1 pom.xml 添加 Test 相关依赖;
2 test / CityWebFluxControllerTest WebFlux API 测试类;
其余业务代码可以使用 03-整合MongoDB 中的代码。
CityWebFluxControllerTest WebFlux API 测试类
@WebFluxTest 用于测试 Spring WebFlux 控制器,支持自动配置 Spring WebFlux 基础组件,可以限制扫描范围等。
代码如下:

代码详解:
1 @WebFluxTest 注入了 WebTestClient 对象,用于测试 WebFlux 控制器,好处是快速,并无需启动完整 HTTP 容器。
2 WebTestClient.post() 方法构造了 POST 测试请求,并使用 uri 指定路由。
3 expectStatus() 用于验证返回状态是否为 ok(),即 200 返回码。
4 expectBody(City.class) 用于验证返回对象体是为 City 对象,并利用 returnResult 获取对象。
5 Assert 是以前我们常用的断言方法验证测试结果。
运行test:

工程运行方式
了解工程服务器部署,先了解工程如何运行。
上面使用应用启动类运行工程,这是其中一种工程运行方式。Spring Boot 应用的运行方式很简单,下面介绍下这三种运行方式。
1. 使用应用启动类
在 IDEA 中直接执行应用启动类,来运行 Spring Boot 应用,日常开发中,会经常使用这种方式启动应用。常用的会有 Debug 启动模式,方便在开发中进行代码调试和 bug 处理。自然,Debug 启动模式会比正常模式稍微慢一些。
2. 使用 Maven 运行
通过 Maven 运行,需要配置 Spring Boot Maven 插件,在 pom.xml 配置文件中,新增 build 节点并配置插件 spring-boot-maven-plugin,代码如下:

在工程根目录中,运行如下 Maven 命令来运行 Spring Boot 应用:
mvn spring-boot:run
实际调用的是 pom.xml 配置的 Spring Boot Maven 插件 spring-boot-maven-plugin,上面执行了插件提供的 run 指令。也可以在 IDEA 右侧工具栏的 Maven Project Tab 中,找到 Maven 插件的 spring-boot-maven-plugin,执行相应的指令。所有指令如下:

3. 使用 Java 命令运行
使用 Maven 或者 Gradle 安装工程,生成可执行的工程 jar 后,运行如下 Java 命令来运行 Spring Boot 应用:
java -jar target/xxx.jar
这里运行了 spring-boot-maven-plugin 插件编译出来的可执行 jar 文件。通过上述三种方式都可以成功运行 Spring Boot 工程,成功运行输出的控制台信息如图 1-10 所示。

工程服务器部署
在服务器中运行需要JDK环境。
Win 服务器推荐使用 AlwaysUp:

使用方式也很简单:

Linux 使用 nohup 命令进行对后台程序的启动关闭。
关闭应用的脚本:stop.sh

启动应用的脚本:start.sh

重启应用的脚本:restart.sh

总结
这一篇主要一起实践了简单的 WebFlux API 控制层的测试,Service 测试 Mock 和以前一样,以及工程运行、服务器部署的操作。
原文:https://gitbook.cn/gitchat/column/5acda6f6d7966c5ae1086f2b/topic/5acdaa90d7966c5ae108709d
代码:https://gitee.com/blueses/springboot-webflux
网友评论