行业资讯 2025年08月6日
0 收藏 0 点赞 794 浏览 2922 个字
摘要 :

文章目录 1.JUnit 5 Gradle依赖项 2. 使用JUnit 5执行JUnit 4测试 3.JUnit 5 Gradle示例 JUnit 5与Gradle。了解如何配置JUnit 5与Gradle,不同的模块以及如何使用它们……




  • 1.JUnit 5 Gradle依赖项
  • 2. 使用JUnit 5执行JUnit 4测试
  • 3.JUnit 5 Gradle示例

JUnit 5与Gradle。了解如何配置JUnit 5与Gradle,不同的模块以及如何使用它们来创建和执行测试。

请注意,JUnit 5在运行时需要Java 8。

1.JUnit 5 Gradle依赖项

要通过Gradle运行JUnit 5测试,您应该至少包含以下依赖项。

  • junit-jupiter-api: 这是所有核心注释所在的主要模块,例如@Test,生命周期方法注释和断言。
  • junit-jupiter-engine: 它具有测试引擎实现,在运行时执行测试时需要。
  • junit-platform-suite: 此模块提供@Suite支持,使JUnitPlatform运行时代理过时。
  • junit-jupiter-params: 对JUnit 5中的参数化测试提供支持。

build.gradle

dependencies {
    testRuntime(\"org.junit.jupiter:junit-jupiter-api:5.8.1\")
    testRuntime(\"org.junit.jupiter:junit-jupiter-engine:5.8.1\")
    testRuntime(\"org.junit.jupiter:junit-jupiter-params:5.8.1\")
    testRuntime(\"org.junit.platform:junit-platform-suite:1.8.1\")
}
test {
    useJUnitPlatform()
}

2. 使用JUnit 5执行JUnit 4测试

要在JUnit 5环境中执行JUnit 4测试,您需要包含junit-vintage-engine依赖项。JUnit Vintage为在平台上运行基于JUnit 4的测试提供了一个TestEngine。

dependencies {
    //运行 junit 4 测试
    testCompile(\"junit:junit:4.12\")
    testRuntime(\"org.junit.vintage:junit-vintage-engine:4.12.0-M4\")
}

通过在build.gradle中配置上述内容,现在您可以使用JUnit 5 Jupiter来运行旧的JUnit 4测试。

3.JUnit 5 Gradle示例

以下是使用junit 5构建测试的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.8.1\'
ext.junitJupiterVersion  = \'5.8.1\'
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 JUnit 4 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\'
}

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9504.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

986
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

463
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

347
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

455
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

515
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

831
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号