• 一文教会你 Spring Boot中的热部署与单元测试(简单易懂,附源码实战)


    需要源码请点赞关注收藏和私信博主

    前言:热部署的简介和目的

    在实际应用开发过程中、业务变化、代码错误等发生时,难免修改程序。为了正确运行出修改的结果,我们往往需要从重启应用,否则将不能看到修改后的效果,这一启动过程时非常浪费时间的,导致开发效率低,因此我们有必要学习Spring Boot开发的热部署,自动实现应用的重启和部署,大大提高 开发和调试效率。

    开发热部署的目的是使应用自动 重启和部署,提高开发效率

    一、模板引擎的热部署

    在Spring Boot应用中,使用模板引擎的页面默认使开启缓存的,如果修改了页面内容,则刷新页面是得不到修改后的页面的效果的。因此可以在配置文件application.properties中关闭模板引擎的缓存

    spring.thymeleaf.cache=false 关闭Thymeleaf的缓存

    spring.freemarker.cache=false 关闭FreeMarker的缓存 

    spring.groovy.template.cache=false 关闭Groovy的缓存 

    二、使用spring-boot-devtools进行热部署

    在Spring Boot应用的pom.xml文件中添加spring-boot-devtools依赖即可实现页面和代码的热部署,其最重要的功能是自动实现将修改的应用代码更新到最新的应用上。

    1:创建Spring Boot Web应用ch9_1

    2:添加spring-boot-devtools依赖

    在pom.xml中添加如下代码

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
    3. <modelVersion>4.0.0</modelVersion>
    4. -<parent>
    5. <groupId>org.springframework.boot</groupId>
    6. <artifactId>spring-boot-starter-parent</artifactId>
    7. <version>2.1.8.RELEASE</version>
    8. <relativePath/>
    9. <!-- lookup parent from repository -->
    10. </parent>
    11. <groupId>com.ch</groupId>
    12. <artifactId>ch9_1</artifactId>
    13. <version>0.0.1-SNAPSHOT</version>
    14. <name>ch9_1</name>
    15. <description>Demo project for Spring Boot</description>
    16. -<properties>
    17. <java.version>11</java.version>
    18. </properties>
    19. -<dependencies>
    20. -<dependency>
    21. <groupId>org.springframework.boot</groupId>
    22. <artifactId>spring-boot-starter-web</artifactId>
    23. </dependency>
    24. -<dependency>
    25. <groupId>org.springframework.boot</groupId>
    26. <artifactId>spring-boot-devtools</artifactId>
    27. </dependency>
    28. -<dependency>
    29. <groupId>mysql</groupId>
    30. <artifactId>mysql-connector-java</artifactId>
    31. <version>5.1.45</version>
    32. <!-- MySQL8.x时,请使用8.x的连接器 -->
    33. </dependency>
    34. <!-- MyBatis-Spring,Spring Boot应用整合MyBatis框架的核心依赖配置 -->
    35. -<dependency>
    36. <groupId>org.mybatis.spring.boot</groupId>
    37. <artifactId>mybatis-spring-boot-starter</artifactId>
    38. <version>2.1.0</version>
    39. </dependency>
    40. -<dependency>
    41. <groupId>org.springframework.boot</groupId>
    42. <artifactId>spring-boot-starter-test</artifactId>
    43. <scope>test</scope>
    44. </dependency>
    45. </dependencies>
    46. -<build>
    47. -<plugins>
    48. -<plugin>
    49. <groupId>org.springframework.boot</groupId>
    50. <artifactId>spring-boot-maven-plugin</artifactId>
    51. </plugin>
    52. </plugins>
    53. </build>
    54. </project>

     3:创建控制器类

    1. package com.ch.ch9_1;
    2. import org.springframework.web.bind.annotation.RequestMapping;
    3. import org.springframework.web.bind.annotation.RestController;
    4. @RestController
    5. public class TestDevToolsController {
    6. @RequestMapping("/testDevTools")
    7. public String testDevTools() {
    8. return "test DevTools 222";
    9. }
    10. }

    4:运行 Ch91Application主类

    然后修改testDevTools方法 改返回值 然后直接刷新页面即可发现结果已经修改 即完成了热部署

    三、Spring Boot的单元测试

    Spring Boot为测试提供 了一个名为spring-boot-starter-test的Starter 使用STS创建Spring Boot应用时将自动添加上述依赖。它主要提供了以下测试库

    1:JNnit:标准的单元测试Java应用程序

    2:Spring Test&Spring Boot Test:针对Spring Boot应用程序的单元测试

    3:Mockito:Java mocking框架 用于模拟任何Spring 管理的Bean

    4:AssertJ:一个流畅的assertion库

    5:JSONassert:对JSON对象或JSON字符串断言的库

    6:JsonPath:提供类似于Xpath那样的符号来获取JSON数据片段

    下面分别通过使用@WebMvcTest和@SpringBootTest两种方式测试一个控制器方法是否满足测试用力

    @WebMvcTest

    1:创建Spring Boot Web应用ch9_2

    2 3步代码可以参照我之前的博客 此处不表

    2:修改pom.xml文件 添加mysql依赖

    3:修改application.properties内容 配置数据库连接 

    4:创建持久化实体类

    1. package com.ch.ch9_2.entity;
    2. import java.io.Serializable;
    3. import javax.persistence.Entity;
    4. import javax.persistence.GeneratedValue;
    5. import javax.persistence.GenerationType;
    6. import javax.persistence.Id;
    7. import javax.persistence.Table;
    8. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    9. @Entity
    10. @Table(name = "student_table")
    11. /**解决No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor异常*/
    12. @JsonIgnoreProperties(value = {"hibernateLazyInitializer"})
    13. public class Student implements Serializable{
    14. private static final long serialVersionUID = 1L;
    15. @Id
    16. @GeneratedValue(strategy = GenerationType.IDENTITY)
    17. private int id;//主键
    18. private String sno;
    19. private String sname;
    20. private String ssex;
    21. public Student() {
    22. super();
    23. }
    24. public Student(int id, String sno, String sname, String ssex) {
    25. super();
    26. this.id = id;
    27. this.sno = sno;
    28. this.sname = sname;
    29. this.ssex = ssex;
    30. }
    31. public int getId() {
    32. return id;
    33. }
    34. public void setId(int id) {
    35. this.id = id;
    36. }
    37. public String getSno() {
    38. return sno;
    39. }
    40. public void setSno(String sno) {
    41. this.sno = sno;
    42. }
    43. public String getSname() {
    44. return sname;
    45. }
    46. public void setSname(String sname) {
    47. this.sname = sname;
    48. }
    49. public String getSsex() {
    50. return ssex;
    51. }
    52. public void setSsex(String ssex) {
    53. this.ssex = ssex;
    54. }
    55. }

    5:创建数据访问层

    1. package com.ch.ch9_2.repository;
    2. import org.springframework.data.jpa.repository.JpaRepository;
    3. import com.ch.ch9_2.entity.Student;
    4. public interface StudentRepository extends JpaRepository<Student, Integer>{
    5. }

    6:创建控制器

    1. package com.ch.ch9_2.controller;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import org.springframework.web.bind.annotation.GetMapping;
    4. import org.springframework.web.bind.annotation.PathVariable;
    5. import org.springframework.web.bind.annotation.PostMapping;
    6. import org.springframework.web.bind.annotation.RequestBody;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import com.ch.ch9_2.entity.Student;
    10. import com.ch.ch9_2.repository.StudentRepository;
    11. @RestController
    12. @RequestMapping("/student")
    13. public class StudentController {
    14. @Autowired
    15. private StudentRepository studentRepository;
    16. /**
    17. * 保存学生信息
    18. */
    19. @PostMapping("/save")
    20. public String save(@RequestBody Student student) {
    21. studentRepository.save(student);
    22. return "success";
    23. }
    24. /**
    25. * 根据id查询学生信息
    26. */
    27. @GetMapping("/getOne/{id}")
    28. public Student getOne(@PathVariable("id") int id){
    29. return studentRepository.getOne(id);
    30. }
    31. }

    7:创建测试用例

    包括基于@WebMvcTest的测试用例WebMvcStudentController

    基于@SpringBootTest的测试用例SpringBootTestStudentController

    需要此处源码可点赞关注收藏后私信博主

  • 相关阅读:
    flowable异步任务加锁流程
    vue.js循环语句
    Go语言并发控制
    POSTGRESQL 从越来越多的ORACLE DBA 考取 PG 证书, 回顾2019- 2022
    从头开始机器学习:神经网络
    基于python+django+vue.js开发的健身房管理系统
    JAVA反射
    Echarts示例
    邮箱发送验证码(nodemailer)
    性能测试 —— 吞吐量和并发量的关系? 有什么区别?
  • 原文地址:https://blog.csdn.net/jiebaoshayebuhui/article/details/127586684