SpringBoot 的 测试类的猫腻

背景

出于打包和效率的考虑,项目上的代码都没有 测试类。在我决定自己写个测试类方面调试时,确发现,自己的测试类竟然无法自动注入Spring容器的bean。⊙(・◇・)?

测试类的目录结构
在这里插入图片描述

原始代码(不可运行)

代码简化,大致就是这个样子。

import com.ajxt.ppd.dao.PpdContractApplyMapper;
import com.ajxt.ppd.po.PpdContractApply;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class AppTest {
    @Autowired
    private PMapper pMapper;
    @Test
    public void tetst(){
        P hhh = pMapper.selectByPrimaryKey("hhh");
        System.out.println(hhh);
    }
}

运行结果,pMapper 直接报了空指针!what fuck?

可运行代码

没办法,只好请教项目组的老大哥帮忙了。然后,他给出了他的测试代码。
什么鬼?@RunWith?测试类还要加这个?

import com.ajxt.ppd.dao.PpdContractApplyMapper;
import com.ajxt.ppd.po.PpdContractApply;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest {
    @Autowired
    private PMapper pMapper;
    @Test
    public void tetst(){
        P hhh = pMapper.selectByPrimaryKey("hhh");
        System.out.println(hhh);
    }
}

正常运行

这么多年了,我一直就是@SpringBootTest就完了啊,甚至我当场建了一个新项目,就是一个@SpringBootTest 就行了啊。ε(┬┬﹏┬┬)3(地铁,老人,问号)
新项目测试类

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@SpringBootTest
class MutiDataSourceApplicationTests {
    @Resource
    PMapper pMapper;
 @Test
    public void tetst(){
        P hhh = pMapper.selectByPrimaryKey("hhh");
        System.out.println(hhh);
    }
}

原因

在我的仔细对比下,还是发现了端倪。两个 @Test导入的包不一样!查询资料,原来是junit的不同版本。

Junit4 -------> org.junit.Test;
Junit5 ----------> org.junit.jupiter.api.Test;

Spring Boot 2.2.0 版本开始引入 JUnit 5作为单元测试默认库,在 Spring Boot 2.2.0 版本之前,spring-boot-starter-test 包含了 JUnit 4 的依赖,Spring Boot 2.2.0 版本之后替换成了 Junit Jupiter。

查看原项目的Springboot版本,
在这里插入图片描述

破案了,搞半天,还是 springboot 版本的锅 !

我就是要用junit5

虽然知道了怎么回事,也能正确测试了,但我还是不甘心,老子就是要用 Junit 5!绝不服输

1. 引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>5.0.2</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.0.2</version>
    <scope>test</scope>
</dependency>

2. 引入测试类

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class AppTest {
    
}

JUnit 5中可以省略@RunWith注解。在JUnit 4中,需要使用@RunWith注解来指定测试运行器,例如@RunWith(SpringRunner.class)来运行Spring的测试运行器。

然而,在JUnit 5中,不再需要使用@RunWith注解。JUnit 5引入了新的扩展模型,通过使用@ExtendWith注解来指定扩展。对于Spring集成测试,可以使用@ExtendWith(SpringExtension.class)注解来启用Spring的测试扩展。这个扩展会自动集成Spring的测试上下文,并确保能够正确注入Spring的bean。

我其实就是想,只用一个 @SpringBootTest搞定测试了而已,怎么去了@RunWith,又加上了@ExtendWith这玩意儿。。。

  • 21
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值