0


Java企业级信息系统开发学习笔记(4.2)Spring Boot项目单元测试、热部署与原理分析

该文章主要为完成实训任务,详细实现过程及结果见【http://t.csdn.cn/pG623】

文章目录


一、Spring Boot单元测试概述

1.1 对项目HelloWorld01进行单元测试

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. 创建测试类与测试方法

  • src/test/java里创建cn.kox.boot包,创建测试类TestHelloWorld01在这里插入图片描述
  • 给测试类添加测试启动器注解与Spring Boot单元测试注解
packagecn.kox.boot;importorg.junit.runner.RunWith;importorg.springframework.boot.test.context.SpringBootTest;importorg.springframework.test.context.junit4.SpringRunner;/**
 * @ClassName: TestHelloWorld01
 * @Author: Kox
 * @Data: 2023/6/13
 * @Sketch:
 */@RunWith(SpringRunner.class)// 实现Spring Boot单元测试@SpringBootTest// 标记Spring Boot测试,并加载应用容器publicclassTestHelloWorld01{}
  • 注入待测试类HelloController在这里插入图片描述
  • 创建测试方法testHello(),测试待测试类实例的hello()方法
@TestpublicvoidtestHello(){// 获取控制器hello()方法的返回值String hello = controller.hello();// 在控制台输出hello()方法的返回值System.out.println("hello()方法的返回值:"+ hello);}
  • 运行测试方法testHello()在这里插入图片描述

1.2 对项目HelloWorld02进行单元测试

1. 添加单元测试依赖

  • pom.xml文件里,添加单元测试依赖在这里插入图片描述
  • 更新项目依赖在这里插入图片描述

2. 进行单元测试

  • 在默认的应用测试类里,注入待测试类,创建testHello()测试方法
packagecn.kox.boot.helloworld02;importcn.kox.boot.controller.HelloController;importorg.junit.Assert;importorg.junit.jupiter.api.Test;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;@SpringBootTestclassHelloWorld02ApplicationTests{@Autowired// 注入待测试类privateHelloController controller;@TestpublicvoidtestHello(){String hello = controller.hello();Assert.assertSame("<h1 style='color: red; text-align: center'>你好,Spring Boot世界~</h1>", hello);}}
  • 运行测试方法testHello(),查看结果在这里插入图片描述

二、Spring Boot热部署

2.1 对项目HelloWorld01进行热部署

1. 添加开发工具依赖

  • 在pom.xml文件里添加开发工具依赖在这里插入图片描述
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency>

2. 热部署配置

在这里插入图片描述

3. 热部署测试

  • 运行入口类HelloWorld01Application在这里插入图片描述
  • 在浏览器里访问http://localhost:8080/kox/hello在这里插入图片描述

本文转载自: https://blog.csdn.net/Kox_233/article/details/131180724
版权归原作者 Kox2021 所有, 如有侵权,请联系我们删除。

“Java企业级信息系统开发学习笔记(4.2)Spring Boot项目单元测试、热部署与原理分析”的评论:

还没有评论