• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

教会你 Spring Boot的热部署和单元测试,附源码

武飞扬头像
showswoller
帮助1

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

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

在实际应用开发过程中、业务变化、代码错误等发生时,难免修改程序。为了正确运行出修改的结果,我们往往需要从重启应用,否则将不能看到修改后的效果,这一启动过程时非常浪费时间的,导致开发效率低,因此我们有必要学习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.  
     
  3.  
    -<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">
  4.  
     
  5.  
    <modelVersion>4.0.0</modelVersion>
  6.  
     
  7.  
     
  8.  
    -<parent>
  9.  
     
  10.  
    <groupId>org.springframework.boot</groupId>
  11.  
     
  12.  
    <artifactId>spring-boot-starter-parent</artifactId>
  13.  
     
  14.  
    <version>2.1.8.RELEASE</version>
  15.  
     
  16.  
    <relativePath/>
  17.  
     
  18.  
    <!-- lookup parent from repository -->
  19.  
     
  20.  
     
  21.  
    </parent>
  22.  
     
  23.  
    <groupId>com.ch</groupId>
  24.  
     
  25.  
    <artifactId>ch9_1</artifactId>
  26.  
     
  27.  
    <version>0.0.1-SNAPSHOT</version>
  28.  
     
  29.  
    <name>ch9_1</name>
  30.  
     
  31.  
    <description>Demo project for Spring Boot</description>
  32.  
     
  33.  
     
  34.  
    -<properties>
  35.  
     
  36.  
    <java.version>11</java.version>
  37.  
     
  38.  
    </properties>
  39.  
     
  40.  
     
  41.  
    -<dependencies>
  42.  
     
  43.  
     
  44.  
    -<dependency>
  45.  
     
  46.  
    <groupId>org.springframework.boot</groupId>
  47.  
     
  48.  
    <artifactId>spring-boot-starter-web</artifactId>
  49.  
     
  50.  
    </dependency>
  51.  
     
  52.  
     
  53.  
    -<dependency>
  54.  
     
  55.  
    <groupId>org.springframework.boot</groupId>
  56.  
     
  57.  
    <artifactId>spring-boot-devtools</artifactId>
  58.  
     
  59.  
    </dependency>
  60.  
     
  61.  
     
  62.  
    -<dependency>
  63.  
     
  64.  
    <groupId>mysql</groupId>
  65.  
     
  66.  
    <artifactId>mysql-connector-java</artifactId>
  67.  
     
  68.  
    <version>5.1.45</version>
  69.  
     
  70.  
    <!-- MySQL8.x时,请使用8.x的连接器 -->
  71.  
     
  72.  
     
  73.  
    </dependency>
  74.  
     
  75.  
    <!-- MyBatis-Spring,Spring Boot应用整合MyBatis框架的核心依赖配置 -->
  76.  
     
  77.  
     
  78.  
     
  79.  
    -<dependency>
  80.  
     
  81.  
    <groupId>org.mybatis.spring.boot</groupId>
  82.  
     
  83.  
    <artifactId>mybatis-spring-boot-starter</artifactId>
  84.  
     
  85.  
    <version>2.1.0</version>
  86.  
     
  87.  
    </dependency>
  88.  
     
  89.  
     
  90.  
    -<dependency>
  91.  
     
  92.  
    <groupId>org.springframework.boot</groupId>
  93.  
     
  94.  
    <artifactId>spring-boot-starter-test</artifactId>
  95.  
     
  96.  
    <scope>test</scope>
  97.  
     
  98.  
    </dependency>
  99.  
     
  100.  
    </dependencies>
  101.  
     
  102.  
     
  103.  
    -<build>
  104.  
     
  105.  
     
  106.  
    -<plugins>
  107.  
     
  108.  
     
  109.  
    -<plugin>
  110.  
     
  111.  
    <groupId>org.springframework.boot</groupId>
  112.  
     
  113.  
    <artifactId>spring-boot-maven-plugin</artifactId>
  114.  
     
  115.  
    </plugin>
  116.  
     
  117.  
    </plugins>
  118.  
     
  119.  
    </build>
  120.  
     
  121.  
    </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.  
    }

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

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfhacka
系列文章
更多 icon
同类精品
更多 icon
继续加载