关于JUnit5,前辈们的文章写的都很好,比如:一篇教你学会Junit5。
或者直接查看官网,内容非常详细:https://junit.org/junit5/docs/current/user-guide/#overview
1. JUnit5模块介绍
官方用户手册:https://junit.org/junit5/docs/current/user-guide/
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
-
JUnit Platform
:JUnit测试平台,JUnit5提供了TestEngine API
,即让开发者自定义一套测试框架,运行在JUnit Platform上(文档:TestEngine在线文档)。如:
-Console Launcher
-JUnit Platform Suite Engine
。 -
JUnit Jupiter
:国外人的命名方式,Jupiter表示木星,用以指代JUnit5核心版本的名称。即是JUnit5对上述TestEngine API
的实现,Jupiter提供了一套可以在JUnit Platform上运行的TestEngine
。 -
JUnit Vintage
:Vintage的意思是古典的,这个模块则是提供了可以让JUnit3和JUnit4运行在上述的JUnit Platform的TestEngine
。
【总结】
JUnit5 三大模块,其中Platform是测试的平台(相当于SPI),Jupiter则是JUnit5在Platform的实现,Vintage测是JUnit3和JUnit4在Platform的实现。
2. 注解(Annotations)
文档:https://junit.org/junit5/docs/current/user-guide/#writing-tests-annotations
JUnit5支持的注解非常多,这里列举几个:
-
@Test
:和JUnit4中的注解差不多,只是package不一样了。
- JUnit4中的Test注解位于包org.junit.Test
下
- JUnit5中的Test注解则位于包org.junit.jupiter.api.Test
下。 -
@DisplayName
:为测试类或方法起名 -
@BeforeAll
:在所有测试方法运行前运行 -
@BeforeEach
:每个测试方法运行前运行 -
@AfterEach
:每个测试方法运行完毕后运行 -
@AfterAll
:在所有测试方法运行完毕后运行 -
@Disabled
:跟JUnit4中的@Ignore一样,被标注的方法不会被运行。
3. 断言(Assertions)
文档:https://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions
所有的JUnit Jupiter断言都是static方法,位于org.junit.jupiter.api.Assertions类中。断言的方法有很多,这里截图只是一部分:![](https://img.haomeiwen.com/i15522532/d83d925c1e9c01a7.png)
除了JUnit Jupiter自带的断言外,官网也推荐其它的第三方断言框架,如:
为什么说官网也推荐其它的第三方断言框架呢?因为第三方断言框架提供的断言功能更多、可读性更强。比如JUnit Jupiter没有assertThat()
方法。而Hamcrest框架则有类似assertThat()
, is()
, 以及equalTo()
的方法。
assertThat(calculator.subtract(4, 1), is(equalTo(3)));
4. 假设(Assumptions)
文档:https://junit.org/junit5/docs/current/user-guide/#writing-tests-assumptions
JUnit Jupiter的Assumption方法,和上述的断言方法一样,是static方法,位于org.junit.jupiter.api.Assumptions
类中。
Assumptions类所有的方法截图如下:
![](https://img.haomeiwen.com/i15522532/912767398e4d3f09.png)
@Test
void testOnlyOnCiServer() {
assumeTrue("CI".equals(System.getenv("ENV")));
// remainder of test
}
如果assumeTrue中的语句不为true,则会抛出:
org.opentest4j.TestAbortedException: Assumption failed: assumption is not true
5. 按条件执行
在包org.junit.jupiter.api.condition
下预置了很多条件,可以按这些条件执行或是disable。
![](https://img.haomeiwen.com/i15522532/024f5ee8924b26ed.png)
如:
@Test
@EnabledOnOs(MAC)
void onlyOnMacOs() {
// ...
}
或:
@Test
@EnabledOnJre(JAVA_8)
void onlyOnJava8() {
// ...
}
6. 其它高级功能
此外,JUnit5 Jupiter还有很多其它功能,具体可以在官网上查看:https://junit.org/junit5/docs/current/user-guide/#writing-tests-tagging-and-filtering
![](https://img.haomeiwen.com/i15522532/9467490b065ced1a.png)
7. 依赖
7.1 junit-jupiter
如果我们想用Jupiter的功能,则直接引入依赖即可:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
<version>5.9.1</version>
</dependency>
它会进而帮忙引入:
![](https://img.haomeiwen.com/i15522532/58b3e4c76e3f1d8e.png)
但是为了更好的管理依赖,我们可以通过junit-bom
来进行管理:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
![](https://img.haomeiwen.com/i15522532/ecb375e65b3dbaf0.png)
更多关于maven-bom,可参考:https://reflectoring.io/maven-bom/
7.2 运行多个依赖
如果想同时运行多个TestEngine,如jupiter和vintage同时存在,那么使用junit-bom就会很方便:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.9.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
7.3 与Spring Boot集成
如果你使用的是Spring Boot,可直接引入spring-boot-starter-test
,Spring Boot会帮忙解决测试相关的依赖,如JUnit, Mockito, AssertJ等。
官方不推荐在Spring Boot项目中使用dependencyManagement的junit-bom来管理依赖。具体可以查看:https://junit.org/junit5/docs/current/user-guide/#running-tests-build-spring-boot
关于Spring Boot的各个版本的默认使用JUnit4还是5,可参考我之前的文章:【测试相关】Spring Boot测试介绍,与JUnit4, Junit5的集成
网友评论