通过Spring Initializr生成的SpringBoot项目

spring-boot-starter-test 都会排除 junit-vintage-engine依赖

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency>

一开始不明白为什么,直到项目踩了坑才知道这很有必要

artifactId 作用
junit-vintage-engine 用于运行JUnit 4 引擎测试
junit-jupiter-engine 用于运行JUnit 5 引擎测试

下面这段测试用例

public class UserMapperTest extends BaseTest {@Autowiredprivate UserMapper userMapper;@Testpublic void countByExample() {final long count = userMapper.countByExample(new UserExample());Assertions.assertTrue(count>0);}
}
问题描述:

运行的时候唯独这个测试用例一直报 userMapper 为null,但是其他的测试用例都是正常的

排查过程:

各种关于spring依赖注入情况都排查了,就是没办法,直到把关注点放到了@Test这个注解

如果spring-boot-starter-test 没有 排除 junit-vintage-engine依赖,那么会导致
org.junit.jupiter.api.Test 和 org.junit.Test 同时存在,如果不小心引入了org.junit.Test ,那么就会发生这个spring 属性注入为null的情况

import org.junit.jupiter.api.Test;
import org.junit.Test;
结论:

1、Spring Boot项目推荐编写JUnit 5测试用例,所以不需要JUnit 4引擎。
2、开发者在引入包时,有两个选择,不小心导错包导致出现MockMvc和WebApplicationContext无法加载,间接导致出现属性为null的情况
综上所述,默认将依赖项排除在外。

附录BaseTest


import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.extension.ExtendWith;
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.web.servlet.MockMvc;
import org.springframework.web.context.WebApplicationContext;@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Application.class)
@DisplayName("Base Test")
public class BaseTest {@Autowiredprotected WebApplicationContext wac;protected MockMvc               mockMvc;@org.junit.jupiter.api.BeforeEachpublic void setUp() throws Exception {mockMvc = webAppContextSetup(wac).build();}}

junit-vintage-engine和junit-jupiter-engine之间的区别?相关推荐

  1. org.junit.vintage.engine.descriptor.RunnerTestDescriptor warnAboutUnfilterableRunner

    org.junit.vintage.engine.descriptor.RunnerTestDescriptor warnAboutUnfilterableRunner 使用springBoot 测试 ...

  2. Failed to resolve org.junit.vintage:junit-vintage-engine:5.6.2

    Error running 'Springboot02ApplicationTests.contextLoads': Failed to resolve org.junit.vintage:junit ...

  3. junit:junit_简而言之,JUnit:测试结构

    junit:junit 尽管存在有关JUnit测试的书籍和文章,但我仍然经常遇到程序员,他们至多对这个工具及其正确用法都不甚了解. 因此,我想到了编写多部分教程的想法,从我的角度解释了要点. 也许在本 ...

  4. junit链接mysql_java – 使用JUnit进行简单的JDBC连接测试

    我想简单测试JDBC连接,我不使用框架,只使用JDBC和JUnit.我可以用JUnit执行此测试吗?我不知道如何测试加载驱动程序,请给我一些连接测试的例子. 连接客户端: package newpac ...

  5. junit:junit_简而言之,JUnit:Hello World

    junit:junit 对于Java世界中的开发人员而言, JUnit似乎是最受欢迎的测试工具 . 因此,难怪就此主题已经写了一些好书 . 但是,通过以顾问为生,我仍然经常遇到程序员,他们至多对工具及 ...

  6. eclipse 环境 JUnit 测试框架(junit.framework.* 与 org.junit.*)

    如下所示,先通过 build path 导入 junit 环境依赖的 jar 包: 1. junit.framework.* junit.framework.* 主要类和函数: Test TestCa ...

  7. 【android】AS中使用Junit单元测试和Android JUnit 单元测试

    AS中使用Junit单元测试和Android JUnit 单元测试 在AndroidStudio中使用单元测试 1. 前言 在Android开发中,如果对一个简单的功能,每次修改代码都重新运行到设备中 ...

  8. 解决方案和项目的区别_沃尔玛用大数据提高销售额,云计算和大数据技术之间的区别汇总...

    自从<纽约时报>发表有关沃尔玛如何利用大数据分析来最大化其销售额的文章以来,人们就对大数据充满了狂热.零售商发现飓风期间流行的糖果品牌Pop-Tarts的销量激增,并利用此知识增加了利润. ...

  9. Spring Boot集成测试中@ContextConfiguration和@SpringApplicationConfiguration之间的区别

    即使同时使用@ContextConfiguration和@SpringApplicationConfiguration批注以及SpringJUnit4ClassRunner来指定如何加载Spring应 ...

最新文章

  1. 深度学习中的验证集和超参数简介
  2. SAP RETAIL 特征参数文件(Characteristic Profile) I
  3. python简单代码画图-Python科学画图代码分享
  4. 张衡的地震仪不起作用的原因
  5. 01_数据库连接池,数据源,ResultSetMetaData,jdbc优化
  6. android.process.media+sd,android P系统WRITE_MEDIA_STORAGE添加读写SD卡权限
  7. 238.除自身以外数组的乘积
  8. Linux内核分析学习心得
  9. Blender Reference Manual 欢迎使用Blender手册!
  10. msm 关机充电开启与关闭
  11. mac1200r 服务器无响应,水星路由器mac1200r桥接不成功怎么办
  12. 使用wps的邮件合并和文档附件制作以照片形式的准考证
  13. scratch 好玩的大鱼吃小鱼
  14. 1.初始PR,怎么导入素材和添加效果处理?
  15. cookie和token身份验证
  16. 如何高效阅读技术论文
  17. 打开带参数的exe方法
  18. C语言预处理、宏定义
  19. WWDC 2015后果
  20. maven引用第三方jar包的解决方法

热门文章

  1. Unity——用代码实现序列帧动画
  2. Mac下安装MacPorts卡住了,“正在运行软件包脚本”,完美解决!
  3. 如何在不同的窗口中打开多个Excel
  4. 如何成为一名JAVA(高级)工程师
  5. 自定义View入门实战案例详析 | 蜘蛛网DataShowView
  6. 一次Linux系统被攻击的分析过程
  7. 【存储测试】cosbench存储性能测试工具
  8. vue动态style添加背景图backgroundImage,结合computed
  9. LUN与LVM的关系
  10. Windows平台mantis安装