Spring Boot单元测试,热部署

(一)对项目HelloWorld01进行单元测试
1.添加依赖

<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里创建net.hw.lesson01包创建测试类TestHelloWorld01
在这里插入图片描述
给测试类添加测试启动器注解与Spring Boot单元测试注解

package net.jyq.lesson1;

import org.junit.Assert;
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;

@RunWith(SpringRunner.class)
@SpringBootTest
class HelloWorld02ApplicationTests {
    @Autowired
    private  HelloWorld02Application controller;
    @Test
    void contextLoads() {
        String hello=controller.hello();
        Assert.assertSame("<h3>你好,Spring Boot世界!</h3>",hello);
    }

}

创建测试方法

@Test
public void testHello(){
    // 获取控制器hello()方法的返回值                            
    String hello = controller.hello();
    // 在控制台输出hello()方法的返回值                           
    System.out.println("hello()方法的返回值:" + hello);
}                                      

在这里插入图片描述
测试方法
在这里插入图片描述
在这里插入图片描述
修改测试方法,利用Assert类的assertSame()方法来进行测试
在这里插入图片描述
在这里插入图片描述
运行结果,测试成功,实际值与期望值相同
在这里插入图片描述
(二)对项目HelloWorld02进行单元测试
在测试类中写入测试方法
在这里插入图片描述
在HelloWorld02Application写入,并创建hello方法

package net.jyq.lesson1;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorld02Application {

    public static void main(String[] args) {
        SpringApplication.run(HelloWorld02Application.class, args);
        SpringApplication app = new SpringApplication(HelloWorld02Application.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
        SpringApplication.run(HelloWorld02Application.class, args);
    }

    public String hello() {
        return "<h3>你好,Spring Boot世界!</h3>";
    }

}

运行绿色代表成功在这里插入图片描述
(三)热部署

深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader加载会更改的类,称为restart
ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个restart
ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。

方法:
1.使用springloaded配置pom.xml文件,使用mvn spring-boot:run启动

2.使用springloaded本地加载启动,配置jvm参数

-javaagent:<jar包地址> -noverify

3.使用devtools工具包,操作简单,但是每次需要重新部署
这里主要讲解一下第三种热部署方式的使用,因为在网上查找资源时,总会发现很多人就是springloaded和devtools都使用了,其实是多余的操作,而且第三种操作简单快捷。

1.对项目HelloWorld01进行热部署
1.1在pom.xml文件添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
</dependency>

1.2热部署
单击[file]->[settings]菜单项,打开设置对话框,找到complier,勾选择build project automatically
在这里插入图片描述
按组合键Ctrl + Shift + Alt + /打开Maintenance对话框
在这里插入图片描述
在Registry对话框里,勾选compiler.automake.allow.when.app.running,让程序在运行过程中也能自动编译在这里插入图片描述
1.3热部署测试
在这里插入图片描述
在浏览器里访问http://localhost:8080/hw01/hello
在这里插入图片描述
不用重新运行启动类HelloWorld01Application直接刷新就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值