单元测试与热部署

(1)单元测试

     开发中,每当完成一个功能接口或业务方法的编写后,通常都会借助单元测试验证该功能是否正确。SpringBoot对项目的单元测试提供了很好的支持,在使用时,需要提前在项目的pom.xml文件中添加spring-boot-starter-test测试依赖启动器,可以通过相关注解实现单元测试

演示:

1.添加spring-boot-starter-test测试依赖启动器

在项目的pom.xml文件中添加spring-boot-starter-test测试依赖启动器,示例代码如下:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
</dependency>

注意:使用Spring Initializr方式搭建的Spring Boot项目,会自动加入spring-boot-starter-test测试依赖启动器,无需再手动添加

2.编写单元测试类和测试方法

使用Spring Initializr方式搭建的Spring Boot项目,会在src.test.java测试目录下自动创建与项目主程序启动类对应的单元测试类

@RunWith(SpringRunner.class) // 测试启动器,并加载Spring Boot测试注解
@SpringBootTest  // 标记为Spring Boot单元测试类,并加载项目的ApplicationContext上下文环境
class SpringbootDemoApplicationTests {
 
   @Autowired
   private DemoController demoController;
 
    //自动创建的单元测试方法实例
   @Test
   void contextLoads() {
      String demo = demoController.demo();
      System.out.println(demo);
    }
}
     上述代码中,先使用@Autowired注解注入了DemoController实例对象,然后在contextLoads()方法中调用了DemoController类中对应的请求控制方法contextLoads(),并输出打印结果

                                        <img src="./images/image-20191225135927575.png"alt="image-20191225135927575" style="zoom:67%;" /
                                        **(2)热部署**

     在开发过程中,通常会对一段业务代码不断地修改测试,在修改之后往往需要重启服务,有些服务需要加载很久才能启动成功,这种不必要的重复操作极大的降低了程序开发效率。为此,Spring Boot框架专门提供了进行热部署的依赖启动器,用于进行项目热部署,而无需手动重启项目 

演示:

1.添加spring-boot-devtools热部署依赖启动器

在Spring Boot项目进行热部署测试之前,需要先在项目的pom.xml文件中添加spring-boot-devtools热部署依赖启动器:

<!-- 引入热部署依赖-->
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-devtools</artifactId>
</dependency>
     由于使用的是IDEA开发工具,添加热部署依赖后可能没有任何效果,接下来还需要针对IDEA开发工具进行热部署相关的功能设置 

2. IDEA工具热部署设置

     选择IDEA工具界面的【File】->【Settings】选项,打开Compiler面板设置页面 

<imgsrc="./images/image-20191225140602409.png"alt=“image-20191225140602409” style=“zoom:67%;” />

     选择Build下的Compiler选项,在右侧勾选“Build project automatically”选项将项目设置为自动编译,单击【Apply】→【OK】按钮保存设置 



     在项目任意页面中使用组合快捷键“Ctrl+Shift+Alt+/”打开Maintenance选项框,选中并打开Registry页面,具体如图1-17所示 

<imgsrc="./images/image-20191225141511157.png"alt=“image-20191225141511157” style=“zoom:67%;” />

     列表中找到“compiler.automake.allow.when.app.running”,将该选项后的Value值勾选,用于指定IDEA工具在程序运行过程中自动编译,最后单击【Close】按钮完成设置

这些内容,是从拉勾教育的《Java工程师高薪训练营》里学到的,课程内容非常全面,还有拉勾的内推大厂服务,推荐你也看看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在SpringBoot 3.0中进行单元测试非常简单。以下是进行单元测试的步骤: 1. 导入必要的依赖。在你的项目的pom.xml文件中,添加以下依赖: ```xml <!--测试类依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> ``` 这个依赖将提供所需的测试框架和工具。 2. 编写单元测试类和测试方法。你可以在测试类中使用`@RunWith(SpringRunner.class)`注解来指定测试运行器,并加载Spring Boot测试注解。使用`@SpringBootTest`注解标记测试类,并加载项目的上下文环境ApplicationContext。例如: ```java package com.xin.spring_01; import com.xin.spring_01.controller.FirstController; 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 Spring01ApplicationTests { @Autowired private FirstController firstController; @Test void contextLoads() { //编写测试方法 //可以使用@Autowired注解注入需要测试的类或组件 //执行测试逻辑 } } ``` 在这个例子中,我们注入了`FirstController`类,并在`contextLoads`方法中编写了测试逻辑。 通过以上步骤,你就可以使用SpringBoot 3.0进行单元测试了。记得在测试方法中编写适当的测试逻辑,以确保你的代码能够正确运行和通过测试。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合Junit](https://blog.csdn.net/JHIII/article/details/126303440)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [SpringBoot(3)单元测试热部署](https://blog.csdn.net/qq_42743832/article/details/120026074)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值