不一样于 JUnit3 或者 JUnit4,JUnit 5 由多个模块组成,并三个核心的子项目:html
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintagegit
查看一下官网 go JUnit5,上面有最新版本显示:github
这版本号能够帮助咱们填写下面的依赖 junit-platform-launcher
AND junit-jupiter-engine
AND junit-vintage-engine
:apache
<dependencies> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.7.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency> </dependencies>
为了 mvn test 顺利执行 Junit5 测试用例,那么你须要 Maven 插件 maven-surefire-plugin
:api
<plugins> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins>
这三个子项目,咱们每次都须要引入吗?intellij-idea
JUnit 5 requires Java 8 (or higher) at runtime.maven
若是,咱们是一个新开的项目,须要使用 Junit5,怎么选?ide
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>junit5-jupiter-starter-maven</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit5.version>5.7.1</junit5.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project>
若是,咱们的项目是一个旧项目,用的是 JDK8 + JUnit4,如今新的测试用例想要用 JUnit5 来写,怎么办?测试
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>junit5-migration-maven</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>${maven.compiler.source}</maven.compiler.target> <junit5.version>5.7.1</junit5.version> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> <version>${junit5.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.2</version> </plugin> </plugins> </build> </project>
要开始使用 JUnit5 Platform,您至少须要向项目中添加一个 TestEngine
实现。ui
junit-jupiter-engine
项目包含实现类JupiterTestEngine
,junit-vintage-engine
项目包含实现类VintageTestEngine
。
若是您想用 Jupiter 编写测试,请将测试件 junit-jupiter-engine
添加到 POM 中的依赖项中。这将引入全部必需的依赖项。在这些依赖关系中,就有 junit-jupiter-api
,它包含测试源代码编译所需的类和接口。
咱们一般使用的 @Test,@Disabled 等注解,Assertions 等断言 API 都出自junit-jupiter-api
。咱们在项目中引入依赖
junit-jupiter-engine
时,Maven 就会自动替咱们引入junit-jupiter-api
。
若是您想经过JUnit平台编写和执行JUnit3或4测试,请将 junit-vintage-engine
添加到依赖项中,该引擎可传递地引入(并须要)junit:junit:4.12
引入依赖junit-vintage-engine
时,Maven 就会自动替咱们引入junit
IntelliJ IDEA在IDEA 2017.3 以前发布了 JUnit 5 的特定捆绑包版本。所以,若是您想使用较新版本的Junit Jupiter,IDE中的测试执行可能会因为版本冲突而失败。在这种状况下,请按照下面的说明使用比 IntelliJ IDEA 捆绑的JUnit 5更新的版本。
junit-platform-launcher
更多的是服务于 IDE 的,若是出现了版本冲突引发的测试执行失败,就须要添加,反之我以为不怎么须要呢?
所以,加或者不加这个 junit-platform-launcher
依赖都有必定的合理性。我的没有遇过这类问题,因此暂时不加。