栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot学习之路(二)——单元测试与热部署

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot学习之路(二)——单元测试与热部署

一、单元测试

在完成一个功能接口或业务方法的编写后,通常借助单元测试验证接口的是否正确。SpringBoot提拱了单元测试的支持。

1、引入spring-boot-starter-test测试依赖启动器


    org.springframework.boot
    spring-boot-starter-test
    test

2、在testxia编写单元测试类与测试方法

单元测试类注解说明

@Runwith(SpringRunner.class)//测试运行器,加载SpringBoot测试注解@SpringBootTest

@SpringBootTest//标记单元测试类,加载项目的上下文环境ApplicationContext

@Test//标记测试方法

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
​
@RunWith(SpringRunner.class)//测试运行器,加载SpringBoot测试注解@SpringBootTest
@SpringBootTest//标记单元测试类,加载项目的上下文环境ApplicationContext
public class Demo01ApplicationTest {
​
    @Autowired//注入HelloController实例对象
    private HelloController helloController;
​
    @Test//标记测试方法
    public void  test(){
        String result = helloController.hello();
        System.out.println(result);
    }
}

3、运行测试方法

 

二、热部署

在编写代码的时候,通常需要不断修改代码测试,但是修改后的代码需要重启服务器才会生效。如果项目比较大的情况下,重启项目比较费时间,于是SpringBoot提供了热部署的依赖启动器,用于进行项目的热部署,而不需要开发人员手动重启服务

1、引入热部署依赖启动器


    org.springframework.boot
    spring-boot-devtools

2、IDEA工具进行热部署设置

第一步: 在【File】--->【Settings】--->【Build,Execution,Deployment】--->【Complier】,勾选Build project automatically(自动编译),单击【Apply 】--->【OK】

第二步:在项目任意页面使用组合键“Ctrl+Shift+Alt+/”,打开Maintenance选项框,打开Registry界面

找到“compiler.automake.allow.when.app.running”,勾选对应的Value值将程序运行方式设置为自动编译,然后单击【Close】

此后修改代码便不需要重启服务器,就能看到修改后的代码

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/571426.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号