目录标题
一、Spring Boot单元测试
(一)Spring Boot单元测试概述
在实际开发中,每当完成一个功能接口或业务方法的编写后,通常都会借助单元测试验证该功能是否正确。Spring Boot对项目的单元测试提供了很好的支持,在使用时,需要提前在项目的pom.xml文件中添加spring-boot-starter-test测试依赖启动器,可以通过相关注解实现单元测试。
(二)对项目HelloWorld01进行单元测试
1、修改pom.xml文件,添加测试依赖启动器和单元测试
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.lsy.lesson01</groupId>
<artifactId>HelloWorld01</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<dependencies>
<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>
</dependencies>
</project>
2、创建测试类与测试方法
运行测试方法testHello()
修改测试方法testHello()
运行测试方法,查看结果
二、Spring Boot热部署
(一)Spring Boot热部署概述
在开发过程中,通常会对一段业务代码不断地修改测试,在修改之后往往需要重启服务,有些服务需要加载很久才能启动成功,这种不必要的重复操作极大地降低了程序开发效率。为此,Spring Boot框架专门提供了进行热部署的依赖启动器,用于进行项目热部署,而无需手动重启项目。
(二)对项目HelloWorld01进行热部署
1、在pom.xml文件里添加开发工具依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
2、热部署配置
- 单击[file]->[settings]菜单项,打开设置对话框,找到
complier
,勾选择build project automatically
(自动构建项目)
按组合键Ctrl + Shift + Alt + /
打开Maintenance
对话框
单击Registry选项,在Registry对话框里,勾选compiler.automake.allow.when.app.running
,让程序在运行过程中也能自动编译
3、热部署测试
- 运行启动类HelloWorld01Application
在浏览器里访问http://localhost:8080/lsy01/hello
修改控制器HelloWorld01Controller代码
不用重新运行启动类HelloWorld01Application,刷新浏览器,查看结果