学习 如何使用 Gradle 配置 JUnit 6。本教程讨论了我们需要构件,如何设置我们的 build.gradle,以及如何执行测试。由于 JUnit 6 构建在 JUnit Platform 之上,因此我们必须确保类路径上存在 TestEngine。
请注意,JUnit 6 需要 Java 17 的最低版本以及 Surefire / Failsafe 插件 3.0.0(或更高版本)才能运行测试。
1. JUnit 6 模块
下表总结了 JUnit 6 中包含的模块及其用途
| 构件 | 描述 | 必需? |
|---|---|---|
| junit-jupiter | 方便的“启动器”构件,它同时引入了 junit-jupiter-api 和 junit-jupiter-engine。 这是使用 JUnit 6 与 Gradle 的最简单方法。 | 是(推荐) |
| junit-jupiter-api | 包含所有公共注解和 API 类,例如 @Test、@BeforeEach、Assertions 等。 用于编译测试是必需的。 | 如果使用 junit-jupiter → 否; 否则 是 |
| junit-jupiter-engine | 实际运行 JUnit 6 测试的 TestEngine 实现。 必须在测试运行时存在。 | 如果使用 junit-jupiter → 否; 否则 是 |
| junit-platform-launcher | 用于以编程方式启动 JUnit Platform。 对自定义测试运行器或 IDE 有用。 | 否 |
| junit-platform-suite-api | 提供类似于 @Suite 的注解来分组和执行测试套件。 | 否 |
| junit-platform-reporting | JUnit Platform 运行的附加报告功能。 | 否 |
| junit-vintage-engine | 启用在 JUnit Platform 上运行遗留的 JUnit 4 和 JUnit 3 测试。 | 可选,仅用于旧的测试支持 |
| junit-bom | BOM(物料清单) 用于自动对齐所有 JUnit 6 模块版本。 | 推荐 但不是强制性的 |
2. 最简化的 JUnit 6 Gradle 示例
下面给出了一个示例 build.gradle 文件。
plugins {
id 'java'
}
group = 'com.howtodoinjava'
version = '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
// Dependency management (JUnit 6 BOM)
testImplementation platform("org.junit:junit-bom:6.0.1")
// JUnit 6 Jupiter (API + Engine)
testImplementation "org.junit.jupiter:junit-jupiter"
}
test {
// Equivalent to Maven Surefire running JUnit Platform
useJUnitPlatform()
// Optional logging for readability
testLogging {
events "passed", "skipped", "failed"
}
}
3. 生产就绪的 JUnit 6 Gradle 设置示例
此配置具有以下测试工具支持
| 功能 | 描述 |
|---|---|
| BOM 导入 | 自动对齐所有 JUnit 6 模块版本 |
| Jupiter Engine + 参数 | 完全现代 JUnit 6 功能支持 |
| Suite API | 支持使用 @Suite 注解的测试套件 |
| Launcher API | 允许以编程方式运行测试(IDE、CI 工具) |
| Surefire + Failsafe | 清晰的分离:单元测试 + 集成测试 |
| 并行执行 | 通过 junit-platform.properties 启用 |
| Jacoco 就绪 | 自动代码覆盖率报告 |
| 自定义测试包含项 | 更简洁的文件命名约定 |
plugins {
id 'java'
id 'jacoco' // Optional: JaCoCo coverage
}
group = 'com.example'
version = '1.0.0'
repositories {
mavenCentral()
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}
dependencies {
// JUnit 6 BOM for version alignment
testImplementation platform("org.junit:junit-bom:6.0.1")
// JUnit 6 Jupiter API + Engine
testImplementation "org.junit.jupiter:junit-jupiter"
// Parameterized tests
testImplementation "org.junit.jupiter:junit-jupiter-params"
// Suite API for @Suite test classes
testImplementation "org.junit.platform:junit-platform-suite-api"
// Platform launcher for programmatic test execution
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
// Optional: AssertJ for fluent assertions
testImplementation "org.assertj:assertj-core:3.26.3"
}
// Unit tests (equivalent to Surefire)
tasks.test {
useJUnitPlatform()
// Include patterns equivalent to Maven Surefire configuration
include '**/*Test.class', '**/*Tests.class', '**/*TestCase.class'
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
}
// Integration tests (equivalent to Failsafe)
sourceSets {
integrationTest {
java {
compileClasspath += sourceSets.main.output + configurations.testRuntimeClasspath
runtimeClasspath += output + compileClasspath
srcDir file('src/integrationTest/java')
}
resources.srcDir file('src/integrationTest/resources')
}
}
configurations {
integrationTestImplementation.extendsFrom testImplementation
integrationTestRuntimeOnly.extendsFrom testRuntimeOnly
}
task integrationTest(type: Test) {
description = 'Runs integration tests.'
group = 'verification'
testClassesDirs = sourceSets.integrationTest.output.classesDirs
classpath = sourceSets.integrationTest.runtimeClasspath
useJUnitPlatform()
include '**/*IT.class'
shouldRunAfter test
}
check.dependsOn integrationTest
// JaCoCo configuration
jacoco {
toolVersion = "0.8.12"
}
tasks.jacocoTestReport {
dependsOn tasks.test
reports {
xml.required.set(true)
html.required.set(true)
}
}
4. 结论
由于其模块化架构和统一的 JUnit Platform,使用 Gradle 设置 JUnit 6 相当简单。 通过包含强制性的 junit-jupiter-engine 以及 API、params 和平台组件等可选模块,我们可以组装项目所需的测试堆栈。
祝您学习愉快!!
评论