Spring Boot基础学习笔记02:Spring Boot项目单元测试、热部署与原理分析
一、Spring Boot单元测试
(一)Spring Boot单元测试概述
- 在实际开发中,每当完成一个功能接口或业务方法的编写后,通常都会借助单元测试验证该功能是否正确。Spring Boot对项目的单元测试提供了很好的支持,在使用时,需要提前在项目的pom.xml文件中添加spring-boot-starter-test测试依赖启动器,可以通过相关注解实现单元测试。
(二)对项目进行单元测试
![image-20210618172613734](https://img-blog.csdnimg.cn/img_convert/863784dc511690683955f75c04c9ccab.png)
1、修改pom.xml文件,添加测试依赖启动器和单元测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
2、创建测试类与测试方法
![image-20210618172842895](https://img-blog.csdnimg.cn/img_convert/a0b6ce9c99bd91f4e2f7714d14a17de5.png)
- 给测试类添加测试启动器注解与Spring Boot单元测试注解
@RunWith(SpringRunner.class)
@SpringBootTest
![image-20210618173322942](https://img-blog.csdnimg.cn/img_convert/e0e6e3faf864c152e4e0ef064a986ede.png)
![image-20210618173704217](https://img-blog.csdnimg.cn/img_convert/e1be874a8a37672f5eccb66bc5303633.png)
- 创建测试方法testHello(),测试待测试类实例的hello()方法
![image-20210618173945737](https://img-blog.csdnimg.cn/img_convert/da43b3b0b773cf031d0f353f9172fcca.png)
![image-20210618174054182](https://img-blog.csdnimg.cn/img_convert/5da05f5c808f7cf0b8cdc37b049ee23c.png)
![image-20210618174131573](https://img-blog.csdnimg.cn/img_convert/34804ac53dc7a8f22bbbe3b63be39ffe.png)
二、Spring Boot热部署
(一)Spring Boot热部署概述
- 在开发过程中,通常会对一段业务代码不断地修改测试,在修改之后往往需要重启服务,有些服务需要加载很久才能启动成功,这种不必要的重复操作极大地降低了程序开发效率。为此,Spring Boot框架专门提供了进行热部署的依赖启动器,用于进行项目热部署,而无需手动重启项目。
(二)对项目进行热部署
1、在pom.xml文件里添加开发工具依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
![image-20210618174411246](https://img-blog.csdnimg.cn/img_convert/c79d5a437d2d5b46a66e8c0e4ef3e356.png)
2、热部署配置
- 单击[file]->[settings]菜单项,打开设置对话框,找到
complier
,勾选择build project automatically
(自动构建项目)
![image-20210618174530708](https://img-blog.csdnimg.cn/img_convert/9c138e0fd44034a79df9d03b17fee884.png)
- 按组合键
Ctrl + Shift + Alt + /
打开Maintenance
对话框
![image-20210618174614362](https://img-blog.csdnimg.cn/img_convert/b6fad94d574868d323cd27ea057d9e32.png)
- 单击Registry选项,在Registry对话框里,勾选
compiler.automake.allow.when.app.running
,让程序在运行过程中也能自动编译
![image-20210618174655121](https://img-blog.csdnimg.cn/img_convert/3cb14819287f96e9ba6ac5809c62b25e.png)
3、热部署测试
![image-20210618174915768](https://img-blog.csdnimg.cn/img_convert/1fdfe1e3d2346eb59340d072ca6f27c6.png)
![image-20210618175156329](https://img-blog.csdnimg.cn/img_convert/03c771860e97d0eee12a965d2a38ee03.png)
![image-20210618175140163](https://img-blog.csdnimg.cn/img_convert/62b88b0ac1eb86012f8271a7603a30e6.png)
- 修改控制器DemoController代码,然后点击
![img](https://img-blog.csdnimg.cn/img_convert/91da40459f78b82fac558c4e05ce59ce.png)
![image-20210618175325546](https://img-blog.csdnimg.cn/img_convert/58855750b655a5ebb5868f257e5944d9.png)
- 不用重新运行启动类DemoApplication,刷新浏览器,查看结果
![image-20210618175408729](https://img-blog.csdnimg.cn/img_convert/68a630536160fb992606f5b9cab67240.png)
课后作业
任务1、对上一讲的课后作业StudentInfo项目进行单元测试
1、添加依赖
![image-20210618175600511](https://img-blog.csdnimg.cn/img_convert/8817b69c76975f28ea380577aae71a15.png)
2、编写测试方法
![image-20210618175942142](https://img-blog.csdnimg.cn/img_convert/36aa5aa290dae3e5f96da03981234931.png)
3、运行测试方法,查看结果
![image-20210618180038411](https://img-blog.csdnimg.cn/img_convert/506baf7ed25eec308d17d9d533de3ee7.png)
任务2、按照讲稿实现热部署,并针对项目StudentInfo进行热部署测试
1、添加依赖
![image-20210618175600511](https://img-blog.csdnimg.cn/img_convert/8817b69c76975f28ea380577aae71a15.png)
2、热部署配置
3、热部署测试
![image-20210618180355242](https://img-blog.csdnimg.cn/img_convert/1da7f8ad4b3d8e6afbc88f2f772af817.png)
![image-20210618180412370](https://img-blog.csdnimg.cn/img_convert/65aeacad43364e5267d554e26134172d.png)
![image-20210618180438388](https://img-blog.csdnimg.cn/img_convert/c14e32835c058e981aef2d7575559e60.png)