Spring Boot学习02

一、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,刷新浏览器,查看结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值