JUnit5 和 Gradle
原文: https://howtodoinjava.com/junit5/junit-5-gradle-dependency-build-gradle-example/
了解使用 gradle 配置它的不同模块,以及如何使用它们来创建和执行测试。
请注意,JUnit5 在运行时需要 Java8。
1. JUnit5 Gradle 依赖项
要通过 gradle 运行 JUnit5 测试,您将至少需要两个依赖项。
-
JUnit Jupiter 引擎依赖项
JUnit jupiter 必须具有两个依赖项,即
junit-jupiter-api
和junit-jupiter-engine
。junit-jupiter-api
具有 junit 注解(例如@Test
)以编写测试和扩展名,junit-jupiter-engine
具有测试引擎实现,在运行时需要执行该引擎才能执行测试。在内部,
junit-jupiter-engine
依赖于junit-jupiter-api
,因此添加junit-jupiter-engine
仅将两个依赖项都带入类路径。我们可以在此图像中了解各种 junit jar 之间的内部依赖项。
dependencies { testRuntime("org.junit.jupiter:junit-jupiter-engine:5.5.2") testRuntime("org.junit.platform:junit-platform-runner:1.5.2") } test { useJUnitPlatform() }
-
JUnit 平台运行器依赖项
我们需要
junit-platform-runner
用于在 JUnit4 环境中的 JUnit 平台上执行测试和测试套件。在内部,
junit-platform-runner
依赖于junit-platform-suite-api
和junit-platform-launcher
,因此添加junit-jupiter-engine
仅将所有三个依赖项引入类路径中。testRuntime("org.junit.platform:junit-platform-runner:1.5.2")
2. 使用 JUnit5 执行 JUnit4 测试
要在 JUnit5 环境中执行 JUnit4 测试,您将需要包括junit-vintage-engine
依赖项。 JUnit Vintage 提供了TestEngine
,用于在平台上运行基于 JUnit 3 和 JUnit4 的测试。
dependencies {
//To run JUnit 3 and JUnit4 tests
testCompile("junit:junit:4.12")
testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
}
通过在build.gradle
中进行上述配置,现在您可以使用 JUnit5 运行旧的 junit 3 或 JUnit4 测试。
3. JUnit5 Gradle 示例
用于运行用 JUnit5 构建的测试的示例build.gradle
文件如下:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
}
}
repositories {
mavenCentral()
}
ext.junit4Version = '4.12'
ext.junitVintageVersion = '4.12.2'
ext.junitPlatformVersion = '1.0.2'
ext.junitJupiterVersion = '5.0.2'
ext.log4jVersion = '2.9.0'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.junit.platform.gradle.plugin'
jar {
baseName = 'junit5-gradle-consumer'
version = '1.0.0-SNAPSHOT'
}
compileTestJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
options.compilerArgs += '-parameters'
}
junitPlatform {
// platformVersion '1.0.2'
filters {
engines {
// include 'junit-jupiter', 'junit-vintage'
// exclude 'custom-engine'
}
tags {
// include 'fast'
exclude 'slow'
}
// includeClassNamePattern '.*Test'
}
// configurationParameter 'junit.jupiter.conditions.deactivate', '*'
// enableStandardTestTask true
// reportsDir file('build/test-results/junit-platform') // this is the default
logManager 'org.apache.logging.log4j.jul.LogManager'
}
dependencies {
// JUnit Jupiter API and TestEngine implementation
testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")
// If you also want to support JUnit 3 and JUnit4 tests
testCompile("junit:junit:${junit4Version}")
testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")
// To avoid compiler warnings about @API annotations in JUnit code
testCompileOnly('org.apiguardian:apiguardian-api:1.0.0')
// To use Log4J's LogManager
testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")
// Only needed to run tests in an (IntelliJ) IDE(A) that bundles an older version
testRuntime("org.junit.platform:junit-platform-launcher:${junitPlatformVersion}")
}
task wrapper(type: Wrapper) {
description = 'Generates gradlew[.bat] scripts'
gradleVersion = '4.3.1'
}
参考: Git
将我的问题放在评论部分。
学习愉快!