Spring Boot WebFlux-09——WebFlux 集成测试及部署
前言
在日常工作中,免不了自测 UT,因为覆盖率不达标,是不允许提交测试,那怎么进行 WebFlux 项目的测试呢。@WebFluxTest 是 WebFlux 测试的重要注解。
结构
回到这个工程中,使用 springboot-webflux-3-mongodb 工程,工程如图:
目录核心如下:
- pom.xml 添加 Test 相关依赖;
- test / CityWebFluxControllerTest WebFlux API 测试类;
POM 依赖
pom.xml 添加对应的测试依赖:
<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
CityWebFluxControllerTest WebFlux API 测试类
@WebFluxTest 用于测试 Spring WebFlux 控制器,支持自动配置 Spring WebFlux 基础组件,可以限制扫描范围等。
代码如下:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CityWebFluxControllerTest {
@Autowired
private WebTestClient webClient;
private static Map<String, City> cityMap = new HashMap<>();
@BeforeClass
public static void setup() throws Exception {
City wl = new City();
wl.setId(1L);
wl.setProvinceId(2L);
wl.setCityName("WL");
wl.setDescription("WL IS GOOD");
cityMap.put("WL", wl);
}
@Test
public void testSave() throws Exception {
City expectCity = webClient.post().uri("/city")
.contentType(MediaType.APPLICATION_JSON)
.body(BodyInserters.fromObject(cityMap.get("WL")))
.exchange()
.expectStatus().isOk()
.expectBody(City.class).returnResult().getResponseBody();
Assert.assertNotNull(expectCity);
Assert.assertEquals(expectCity.getId(), cityMap.get("WL").getId());
Assert.assertEquals(expectCity.getCityName(), cityMap.get("WL").getCityName());
}
}
代码详解:
- @WebFluxTest 注入了 WebTestClient 对象,用于测试 WebFlux 控制器,好处是快速,并无需启动完整 HTTP 容器。
- WebTestClient.post() 方法构造了 POST 测试请求,并使用 uri 指定路由。
- expectStatus() 用于验证返回状态是否为 ok(),即 200 返回码。
- expectBody(City.class) 用于验证返回对象体是为 City 对象,并利用 returnResult 获取对象。
- Assert 是以前我们常用的断言方法验证测试结果。
运行 Test,得到如图验证结果:
工程运行方式
了解工程服务器部署,先了解工程如何运行。
上面使用应用启动类运行工程,这是其中一种工程运行方式。Spring Boot 应用的运行方式很简单,下面介绍下这三种运行方式。
1. 使用应用启动类
在 IDEA 中直接执行应用启动类,来运行 Spring Boot 应用,日常开发中,会经常使用这种方式启动应用。常用的会有 Debug 启动模式,方便在开发中进行代码调试和 bug 处理。自然,Debug 启动模式会比正常模式稍微慢一些。
2. 使用 Maven 运行
通过 Maven 运行,需要配置 Spring Boot Maven 插件,在 pom.xml 配置文件中,新增 build 节点并配置插件 spring-boot-maven-plugin,代码如下:
<build>
<plugins>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
在工程根目录中,运行如下 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,执行相应的指令。所有指令如下:
# 生成构建信息文件
spring-boot:build-info
# 帮助信息
spring-boot:help
# 重新打包
spring-boot:repackage
# 运行工程
spring-boot:run
# 将工程集成到集成测试阶段,进行工程的声明周期管理
spring-boot:start
spring-boot:stop
3. 使用 Java 命令运行
使用 Maven 或者 Gradle 安装工程,生成可执行的工程 jar 后,运行如下 Java 命令来运行 Spring Boot 应用:
java -jar target/xxx.jar
这里运行了 spring-boot-maven-plugin 插件编译出来的可执行 jar 文件。通过上述三种方式都可以成功运行 Spring Boot 工程,成功运行输出的控制台信息如图 1-10 所示。
工程服务器部署
基础环境安装如上面说的,需要 JDK 环境、Maven 环境等。
Win 服务器
推荐使用 AlwaysUp:
使用方式也很简单:
Linux 服务器
推荐 yum 安装基础环境,比如安装 JDK:
yum -y list java*
yum -y install java-1.8.0-openjdk*
java -version
安装 Maven:
yum -y list apache-maven
sudo wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
sudo yum install -y apache-maven
mvn --v
Linux 使用 nohup 命令进行对后台程序的启动关闭。
关闭应用的脚本:stop.sh
启动应用的脚本:start.sh
重启应用的脚本:stop.sh
总结
这一篇主要一起实践了简单的 WebFlux API 控制层的测试,Service 测试 Mock 和以前一样,以及工程运行、服务器部署的操作。
工程:springboot-webflux-9-test。
Spring Boot WebFlux-09——WebFlux 集成测试及部署的更多相关文章
- spring boot项目如何测试,如何部署
有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...
- Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏 云收藏项目已经开源3年多了,作为当初刚开始学习 Spring Boot 的练手项目,使用了很多当时 ...
- Spring Boot 2 (四):使用 Docker 部署 Spring Boot
Spring Boot 2 (四):使用 Docker 部署 Spring Boot Docker 技术发展为微服务落地提供了更加便利的环境,使用 Docker 部署 Spring Boot 其实非常 ...
- Spring Boot(十六):使用Jenkins部署Spring Boot
Spring Boot(十六):使用Jenkins部署Spring Boot jenkins是devops神器,介绍如何安装和使用jenkins部署Spring Boot项目 jenkins搭建 部署 ...
- Spring Boot 2.0 WebFlux 教程 (一) | 入门篇
目录 一.什么是 Spring WebFlux 二.WebFlux 的优势&提升性能? 三.WebFlux 应用场景 四.选 WebFlux 还是 Spring MVC? 五.异同点 六.简单 ...
- 实战基于Spring Boot 2的WebFlux和mLab搭建反应式Web
Spring Framework 5带来了新的Reactive Stack非阻塞式Web框架:Spring WebFlux.作为与Spring MVC并行使用的Web框架,Spring WebFlux ...
- Spring Boot 和 Docker 实现微服务部署
Spring boot 开发轻巧的微服务提供了便利,Docker 的发展又极大的方便了微服务的部署.这篇文章介绍一下如果借助 maven 来快速的生成微服务的镜像以及快速启动服务. 其实将 Sprin ...
- (转)Spring Boot 2 (六):使用 Docker 部署 Spring Boot 开源软件云收藏
http://www.ityouknow.com/springboot/2018/04/02/docker-favorites.html 云收藏项目已经开源2年多了,作为当初刚开始学习 Spring ...
- (转)Spring Boot 2 (四):使用 Docker 部署 Spring Boot
http://www.ityouknow.com/springboot/2018/03/19/spring-boot-docker.html Docker 技术发展为微服务落地提供了更加便利的环境,使 ...
- (转)Spring Boot(十六):使用 Jenkins 部署 Spring Boot
http://www.ityouknow.com/springboot/2017/11/11/spring-boot-jenkins.html enkins 是 Devops 神器,本篇文章介绍如何安 ...
随机推荐
- @ResponseBody、@RequestBody
@ResponseBody 我们在刚刚接触Springboot的第一个hello工程的时候,我们就接触了一个RestController,而通过进入它的源码,我们会发现@ResponseBody @R ...
- 利用cm压缩包手动安装cm和cdh
安装准备: 1.操作系统为centos6.9 CentOS-6.9-x86_64-bin-DVD1to2 2.安装Oracle JDK (1.8u121) 下载jdk-8u121-linux-x64. ...
- Java并发编程(二)如何保证线程同时/交替执行
第一篇文章中,我用如何保证线程顺序执行的例子作为Java并发系列的开胃菜.本篇我们依然不会有源码分析,而是用另外两个多线程的例子来引出Java.util.concurrent中的几个并发工具的用法. ...
- NumPy之:ndarray多维数组操作
NumPy之:ndarray多维数组操作 目录 简介 创建ndarray ndarray的属性 ndarray中元素的类型转换 ndarray的数学运算 index和切片 基本使用 index wit ...
- 拦截器(Interceptor)与过滤器(Filter)
目录 用户的普通Http请求执行顺序 过滤器.拦截器添加后的执行顺序 拦截器(Interceptor)的基本定义 拦截器(Interceptor)必须实现的三个方法 单个拦截器(Interceptor ...
- Java7中Switch为什么只支持byte、short、char、int、String
Java 7中,switch的参数可以是String类型了,这对我们来说是一个很方便的改进.到目前为止switch支持这样几种数据类型:byte short int char String .但是,作 ...
- 如何设计一个高性能 Elasticsearch mapping
目录 前言 mapping mapping 能做什么 Dynamic mapping dynamic=true dynamic=runtime dynamic=false dynamic=strict ...
- centos7 连接打印机
centos7 连接打印机 2017-08-07 15:05:33 五岳寻仙客 阅读数 2531更多 分类专栏: Liunx的日常使用 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版 ...
- CrystalDiskMark v7.0.0h中文版
CrystalDiskMark v7.0.0h中文版 发布时间:2020/03/05 05:03:09作者:zhongdong CrystalDiskMark是一个轻量级高效率检测工具,所有内容整齐的 ...
- mysql基础之视图、事务、索引、外键
一.视图 视图是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据.但是,视图并不在数据库中以存储的数据值集形式存在.行和列数据来自由定义视图的查询所引用的表,并且在引用 ...