本操作在Spring Boot实践之三的基础上继续:
1 单元测试
-
1.在pom文件中添加spring-boot-starter-test测试启动器
<!-- 引入单元测试依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
实际开发中,每当完成一个功能接口或业务方法的编写后,通常都会借助单元测试验证该功能是否正确。spring boot通过在pox.xml添加测试依赖启动器后,可以提供很好的支持。
使用spring Initializr方式搭建的spring boot 项目,会自动加入.
-
2.编写单元测试类和测试方法,这里使用spring Initializr方式搭建的上一个案例,在src.test.java测试目录下自动创建的与项目主程序启动类对应单元测试类Chapter01ApplicationTests.java,代码如下:
package com.itheima.chapter01; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * 使用spring Initializr方式自动创建的主程序启动类对应的单元测试类 */ @RunWith(SpringRunner.class) //测试运行器,用于加载Spring Boot测试注解 @SpringBootTest //标记单元测试类,并加载项目的上下文环境ApplicationContext class Chapter01ApplicationTests { @Test void contextLoads() { } }
-
在单元测试类Chapter01ApplicationTests中新增单元测试方法,完整代码如下:
package com.itheima.chapter01; import com.itheima.chapter01.controller.HelloController; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; /** * 使用spring Initializr方式自动创建的主程序启动类对应的单元测试类 */ @RunWith(SpringRunner.class) //测试运行器,并加载Spring Boot测试注解 @SpringBootTest //标记单元测试类,并加载项目的上下文环境ApplicationContext class Chapter01ApplicationTests { @Test void contextLoads() { } @Autowired //本注解注入了HelloController实例对象 private HelloController helloController; @Test public void helloControllerTest() { String hello = helloController.hello();//调用helloController类中对应的请求控制方法hello() System.out.println(hello); //输出打印结果 } }
-
选中单元测试方法helloControllerTest(),鼠标右键单击【Run “helloControllerTest()”】选项启动测试方法,控制台打印信息如图:
2 热部署
为避免对业务代码不断修改测试时,需要重启服务,Spring Boot 框架专门提供了进行热部署的依赖启动器,用于进行项目热部署而无需重启项目。具体步骤:
-
添加spring-boot-devtools热部署依赖启动器
<!-- 引入热部署依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
该方法基于类加载机制来实现热加载的,因此你修改完成代码后必须重新编译当前代码,才能触发热部署.
true课文中没有提及,前期测试热部署无效,添加后有效,再删除后测试也有效.
-
IDEA工具热部署设置
- File-Settings,在打开的面板中选择Build Execution,Deployment->Compiler,勾选Build project automatically和Compile independent modules in parallel,单击 apply->OK按钮完成保存设置,作用是将项目设置为自动编译。
在项目任意页面使用组合键“Ctrl+Shift+Alt+/”,打开Maitenance选项框,选中并打开Registry界面,具体如图所示.
- File-Settings,在打开的面板中选择Build Execution,Deployment->Compiler,勾选Build project automatically和Compile independent modules in parallel,单击 apply->OK按钮完成保存设置,作用是将项目设置为自动编译。
-
在打开的面板打到"compiler.automake.allow.when.app.running",勾选对应的Value值将程序运行方式设置为自动编译,最后单击close按钮完成设置.
-
热部署效果测试
- 启动上一个完成的chapter01项目,通过浏览器访问"http://localhost:8080/hello",具体如图,原始输出的内容为:“hello Spring Boot”.
(说明:图中是多次测试修改后的,多了个!号 - 在不关闭当前项目运行的情况下,将HelloController类中的请求处理方法,修改为"你好!Spring Boot"并保存,查看控制台信息会发现项目上能够自动构建和编译,说明项目热部署生效。刷新浏览器访问"http://localhost:8080/hello"页面(可能要刷新两三次),显示如图:
本文和上一篇Spring Boot实践之三的源代码一并提供,如果大家试运行不成功,可以下载对比。欢迎指正。
- 启动上一个完成的chapter01项目,通过浏览器访问"http://localhost:8080/hello",具体如图,原始输出的内容为:“hello Spring Boot”.