目录
2、SpringJUnit4ClassRunner 与SpringRunner 区别
1、单元测试需要引入的Maven依赖
SpringBoot 首先需要引入 如下SpringBoot基本测试依赖
注意事项:如果SpringBoot是1.X版本引入如下测试即可,默认使用的是Junit4.X版本
<!--springboot程序测试依赖,创建项目默认添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
但是如果你 升级使用的SpringBoot2.X那你的 Junit被使用Junit5.X,于是乎很多网上以前的单元测试内容不管用了,就算你
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
也是会报错的,可能如出现如下情况如下图所示,因为此时你的junit默认为5.X版本了:
于是你需要制定你使用Junit使用版本为4.X
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.18.RELEASE</version>
<scope>test</scope>
</dependency>
此时的单元测试就Ok了,
2、SpringJUnit4ClassRunner 与SpringRunner 区别
我本人看过网上很多关于单元测试,有些人使用SpringRunner.class 有些认识使用 SpringJUnit4ClassRunner.class
在网上特意查询了一下,
SpringRunner is an alias for the SpringJUnit4ClassRunner.
To use this class, simply annotate a JUnit 4 based test class with @RunWith(SpringRunner.class).
----------------------
SpringRunner 继承了SpringJUnit4ClassRunner,没有扩展任何功能;使用前者,名字简短而已。