详细分析SpringBootTest中的测试类(附Demo)

1. 基本知识

涉及测试类的基本注解分析如下:

一、@SpringBootTest:加载完整的Spring应用上下文

使用场景:需要测试整个Spring Boot应用时使用,确保所有的Bean都能被正确加载

kotlin 复制代码
@SpringBootTest
public class MyApplicationTests {
    // 测试方法
}

1234

二、@RunWith(SpringRunner.class):指定测试运行器,这里使用SpringRunner来运行Spring Boot测试

使用场景:结合JUnit 4使用时指定测试运行器

kotlin 复制代码
@RunWith(SpringRunner.class)
public class MyApplicationTests {
    // 测试方法
}

1234

三、@Autowired:自动注入Spring管理的Bean

使用场景:在测试类中需要注入服务、仓库等Bean时使用

java 复制代码
@Autowired
private MyService myService;

12

四、@Slf4j:使用Lombok提供的日志记录功能

使用场景:需要在类中记录日志时使用

kotlin 复制代码
@Slf4j
public class MyApplicationTests {
    // 可以使用log.info(), log.debug()等记录日志
}

1234

五、@Test:标记一个方法为测试方法

使用场景:任何需要进行单元测试的方法都应使用该注解

typescript 复制代码
@Test
public void testMethod() {
    // 测试逻辑
}

1234

常出现的情况有如下:

  1. 注入的Bean为null
    使用@Autowired注入的Bean为null
    》》》》确保被注入的类被Spring管理(例如,使用@Service、@Component等注解),确保测试类使用@SpringBootTest注解以加载完整的Spring上下文
  2. 测试类无法加载上下文
    测试类无法加载Spring上下文,抛出BeanCreationException等异常
    》》》》确保@SpringBootTest注解的classes属性指向正确的主应用程序类,例如AccidentApplication.class

2. Demo

结合实战中的Demo进行讲解

java 复制代码
import com.sinosoft.springbootplus.AccidentApplication;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest(classes = AccidentApplication.class)
@Slf4j
public class TestClass {

    @Autowired
    private XXXService xxxService;

    @Test
    public void testDemo() {
        // 示例测试代码
        log.info("Starting testDemo");
        // 调用XXXService的某个方法,并进行断言
        String result = xxxService.someMethod();
        assertNotNull(result);
        log.info("testDemo completed");
    }
}

123456789101112131415161718192021222324

其中的注意事项如下:

  1. 类路径:确保AccidentApplication类在你的类路径下,并且可以被正确加载
  2. Service注入:XXXService必须在Spring上下文中被定义为一个Bean,否则@Autowired注入会失败
  3. JUnit版本 :确保项目使用JUnit 5(JUnit Jupiter)或者JUnit 4,不要混用
    上面的代码中使用的是JUnit 5,如果你需要使用JUnit 4,请使用org.junit.Test和org.junit.runner.RunWith
  4. 日志使用:通过@Slf4j注解,可以在测试方法中方便地记录日志,这有助于调试

而且基本的项目结构如下:

css 复制代码
src
├── main
│   └── java
│       └── com
│           └── manong
│               └── springbootplus
│                   ├── AccidentApplication.java
│                   └── service
│                       └── XXXService.java
└── test
    └── java
        └── com
            └── manong
                └── springbootplus
                    └── TestClass.java

123456789101112131415

3. 实战

3.1 项目测试

Demo与实战中大同小异,上述使用的是JUnit 5 ,下面使用一个JUnit 4进行演示

java 复制代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = YudaoServerApplication.class)
public class projectTest {

    @Test
    public void testUsingTosDataSource() {
    }

}

123456789101112131415

实际截图如下:

3.2 功能测试

实战中可能有多个模块,对应的配置需如下:

scss 复制代码
package cn.example.module.dangerous.service.enterpriseregistry;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.mock.mockito.MockBean;

import javax.annotation.Resource;

import cn.example.framework.test.core.ut.BaseDbUnitTest;

import cn.example.module.dangerous.controller.admin.enterpriseregistry.vo.*;
import cn.example.module.dangerous.dal.dataobject.enterpriseregistry.EnterpriseRegistryDO;
import cn.example.module.dangerous.dal.mysql.enterpriseregistry.EnterpriseRegistryMapper;
import cn.example.framework.common.pojo.PageResult;

import javax.annotation.Resource;
import org.springframework.context.annotation.Import;
import java.util.*;
import java.time.LocalDateTime;

import static cn.hutool.core.util.RandomUtil.*;
import static cn.example.module.dangerous.enums.ErrorCodeConstants.*;
import static cn.example.framework.test.core.util.AssertUtils.*;
import static cn.example.framework.test.core.util.RandomUtils.*;
import static cn.example.framework.common.util.date.LocalDateTimeUtils.*;
import static cn.example.framework.common.util.object.ObjectUtils.*;
import static cn.example.framework.common.util.date.DateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * {@link EnterpriseRegistryServiceImpl} 的单元测试类
 *
 * @author 管理员
 */
@Import(EnterpriseRegistryServiceImpl.class)
public class EnterpriseRegistryServiceImplTest extends BaseDbUnitTest {

    @Resource
    private EnterpriseRegistryServiceImpl enterpriseRegistryService;

    @Resource
    private EnterpriseRegistryMapper enterpriseRegistryMapper;

    @Test
    public void testCreateEnterpriseRegistry_success() {
        // 准备参数
        EnterpriseRegistrySaveReqVO createReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class).setId(null);

        // 调用
        Long enterpriseRegistryId = enterpriseRegistryService.createEnterpriseRegistry(createReqVO);
        // 断言
        assertNotNull(enterpriseRegistryId);
        // 校验记录的属性是否正确
        EnterpriseRegistryDO enterpriseRegistry = enterpriseRegistryMapper.selectById(enterpriseRegistryId);
        assertPojoEquals(createReqVO, enterpriseRegistry, "id");
    }

    @Test
    public void testUpdateEnterpriseRegistry_success() {
        // mock 数据
        EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class);
        enterpriseRegistryMapper.insert(dbEnterpriseRegistry);// @Sql: 先插入出一条存在的数据
        // 准备参数
        EnterpriseRegistrySaveReqVO updateReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class, o -> {
            o.setId(dbEnterpriseRegistry.getId()); // 设置更新的 ID
        });

        // 调用
        enterpriseRegistryService.updateEnterpriseRegistry(updateReqVO);
        // 校验是否更新正确
        EnterpriseRegistryDO enterpriseRegistry = enterpriseRegistryMapper.selectById(updateReqVO.getId()); // 获取最新的
        assertPojoEquals(updateReqVO, enterpriseRegistry);
    }

    @Test
    public void testUpdateEnterpriseRegistry_notExists() {
        // 准备参数
        EnterpriseRegistrySaveReqVO updateReqVO = randomPojo(EnterpriseRegistrySaveReqVO.class);

        // 调用, 并断言异常
        assertServiceException(() -> enterpriseRegistryService.updateEnterpriseRegistry(updateReqVO), ENTERPRISE_REGISTRY_NOT_EXISTS);
    }

    @Test
    public void testDeleteEnterpriseRegistry_success() {
        // mock 数据
        EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class);
        enterpriseRegistryMapper.insert(dbEnterpriseRegistry);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbEnterpriseRegistry.getId();

        // 调用
        enterpriseRegistryService.deleteEnterpriseRegistry(id);
        // 校验数据不存在了
        assertNull(enterpriseRegistryMapper.selectById(id));
    }

    @Test
    public void testDeleteEnterpriseRegistry_notExists() {
        // 准备参数
        Long id = randomLongId();

        // 调用, 并断言异常
        assertServiceException(() -> enterpriseRegistryService.deleteEnterpriseRegistry(id), ENTERPRISE_REGISTRY_NOT_EXISTS);
    }

    @Test
    @Disabled  // TODO 请修改 null 为需要的值,然后删除 @Disabled 注解
    public void testGetEnterpriseRegistryPage() {
        // mock 数据
        EnterpriseRegistryDO dbEnterpriseRegistry = randomPojo(EnterpriseRegistryDO.class, o -> { // 等会查询到
            o.setEnterpriseName(null);
            o.setCreditCode(null);
            o.setRegistrant(null);
            o.setCreateTime(null);
            o.setEnterpriseCode(null);
            o.setContactNumber(null);
        });
        enterpriseRegistryMapper.insert(dbEnterpriseRegistry);
        // 测试 enterpriseName 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setEnterpriseName(null)));
        // 测试 creditCode 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setCreditCode(null)));
        // 测试 registrant 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setRegistrant(null)));
        // 测试 createTime 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setCreateTime(null)));
        // 测试 enterpriseCode 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setEnterpriseCode(null)));
        // 测试 contactNumber 不匹配
        enterpriseRegistryMapper.insert(cloneIgnoreId(dbEnterpriseRegistry, o -> o.setContactNumber(null)));
        // 准备参数
        EnterpriseRegistryPageReqVO reqVO = new EnterpriseRegistryPageReqVO();
        reqVO.setEnterpriseName(null);
        reqVO.setCreditCode(null);
        reqVO.setRegistrant(null);
        reqVO.setCreateTime(buildBetweenTime(2023, 2, 1, 2023, 2, 28));
        reqVO.setEnterpriseCode(null);
        reqVO.setContactNumber(null);

        // 调用
        PageResult<EnterpriseRegistryDO> pageResult = enterpriseRegistryService.getEnterpriseRegistryPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbEnterpriseRegistry, pageResult.getList().get(0));
    }

}
相关推荐
独自破碎E几秒前
Spring Boot + LangChain4j 报错:Bean 类型不匹配的解决办法
spring boot·python·pycharm
tb_first2 分钟前
SSM速通3
java·jvm·spring boot·mybatis
她说..1 小时前
策略模式+工厂模式实现审批流(面试问答版)
java·后端·spring·面试·springboot·策略模式·javaee
梦梦代码精1 小时前
开源、免费、可商用:BuildingAI一站式体验报告
开发语言·前端·数据结构·人工智能·后端·开源·知识图谱
程可爱2 小时前
springboot整合mybatis和postgresql
spring boot·postgresql·mybatis
李慕婉学姐2 小时前
【开题答辩过程】以《基于Spring Boot的疗养院理疗管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
tb_first3 小时前
SSM速通2
java·javascript·后端
qq_12498707533 小时前
基于协同过滤算法的运动场馆服务平台设计与实现(源码+论文+部署+安装)
java·大数据·数据库·人工智能·spring boot·毕业设计·计算机毕业设计
一路向北⁢3 小时前
Spring Boot 3 整合 SSE (Server-Sent Events) 企业级最佳实践(一)
java·spring boot·后端·sse·通信
风象南3 小时前
JFR:Spring Boot 应用的性能诊断利器
java·spring boot·后端