Spring Boot 单元测试


一,Spring Boot单元测试概述

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

二,对项目HelloWorld01进行单元测试

1、添加测试依赖启动器和单元测试

打开先前创建的项目(如未创建请参考《Maven方式构建Spring Boot项目》) - HelloWorld01
在这里插入图片描述修改pom.xml文件,添加依赖
在这里插入图片描述

添加内容如下:

<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>

刷新项目依赖
在这里插入图片描述
如果使用Spring Initializr方式构建Spring Boot项目,则会自动加入测试依赖启动器。大家可以查看上一讲我们创建的HelloWorld02项目。

2、创建测试类与测试方法

在src/test/java里创建net.army.boot包
在这里插入图片描述
在net.army.boot包里创建测试类TestHelloWorld01
在这里插入图片描述

给测试类添加测试启动器注解与Spring Boot单元测试注解

@RunWith(SpringRunner.class) // 实现Spring Boot单元测试
@SpringBootTest // 标记Spring Boot测试,并加载应用容器

注入待测试类HelloController
在这里插入图片描述
创建测试方法testHello(),测试待测试类实例的hello()方法

添加如下代码:

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

运行测试方法testHello()
在这里插入图片描述如果相判断待测试类的方法的返回值是不是指定的某个数据,那么我们可以利用Assert类的assertSame()方法来进行测试

修改测试方法testHello()
在这里插入图片描述运行测试方法,查看结果
在这里插入图片描述
测试失败。抛出AssertionError(断言错误)。

期望值:Hello Spring Boot World~

实际值:<h1 style='color: red; text-align: center'>Hello Spring Boot World~</h1>

再修改测试方法testHello(),修改期望值
在这里插入图片描述
运行测试方法,查看结果
在这里插入图片描述

三,对项目HelloWorld02进行单元测试

1、添加单元测试依赖

打开先前创建的项目(如未创建请参考《Spring Initializr方式构建Spring Boot项目》),查看Spring Initializr自动生成的测试类
在这里插入图片描述

在pom.xml文件里,添加单元测试依赖
在这里插入图片描述
添加内容如下:

<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <scope>test</scope>
</dependency>

更新项目依赖
在这里插入图片描述

2、进行单元测试

在默认的应用测试类里,注入待测试类,创建testHello()测试方法
在这里插入图片描述

添加代码如下:

package net.army.boot;

import net.army.boot.controller.HelloController;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class HelloWorld02ApplicationTests {
    @Autowired // 注入待测试类
    private HelloController controller;
    @Test
    public void testHello() {
        String hello = controller.hello();
        Assert.assertSame("<h1 style='color: red; text-align: center'>你好,Spring Boot世界~</h1>", hello);
    }
}

运行测试方法testHello(),查看结果
在这里插入图片描述

要配置SpringBoot单元测试,可以按照以下步骤进行操作: 1. 引入依赖:在项目的pom.xml文件中添加Spring Boot测试的依赖项。可以使用以下代码片段将依赖项添加到pom.xml文件中: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> ``` 这些依赖项将提供执行单元测试所需的必要库和工具。 2. 添加注解:在需要进行测试的类上添加注解。使用`@RunWith`注解将测试运行器设置为SpringRunner,并使用`@SpringBootTest`注解指定要启动的Spring Boot应用程序的入口类。以下是一个示例: ```java import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes={YourApplication.class}) public class YourTestClass { // 测试方法 } ``` 在上述示例中,`YourApplication.class`应替换为你的Spring Boot应用程序的实际入口类。 3. 编写测试方法:在测试类中编写测试方法,并使用`@Test`注解标记这些方法作为测试方法。你可以在这些方法中编写针对不同功能的测试用例。 这样配置后,你可以使用IDE或者Maven等工具运行单元测试Spring Boot会自动加载应用程序上下文并执行测试方法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot单元测试之配置流程](https://blog.csdn.net/qq_38058674/article/details/123058717)[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_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁辰兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值