简体   繁体   English

在Gradle / JUnit中启用断言

[英]Enabling assertions in Gradle/JUnit

My project uses gradle and JUnit 5.01. 我的项目使用gradle和JUnit 5.01。 The JUnit assertions are working fine. JUnit断言工作正常。 However, my regular Java assertions in the tested code itself are not firing. 但是,我在测试代码中的常规Java断言并未触发。 I would expect a failed assert to throw an AssertionError that would be caught and reported by JUnit. 我希望失败的断言能够抛出一个将被JUnit捕获并报告的AssertionError。

I found this: How to disable assert in gradle test , and so created this build.gradle: 我发现了这个: 如何在gradle测试中禁用assert ,因此创建了这个build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
    }
}

apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'maven'
apply plugin: 'org.junit.platform.gradle.plugin'

compileJava {
    options.compilerArgs += "-Xlint:unchecked"
}

tasks.withType(Test) {
    enableAssertions = true
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api:5.0.1')
    testCompile('org.apiguardian:apiguardian-api:1.0.0')
    testRuntime('org.junit.jupiter:junit-jupiter-engine:5.0.1')
}

// Define the main class for the application
mainClassName = 'CMS'

jar {
    manifest {
        attributes 'Implementation-Title': 'CMS',
                   'Main-Class': 'com.brandli.cms.CMS'
    }
}

junitPlatform {
    filters {
        includeClassNamePattern '.*'
    }
}

test {
    testLogging {
        exceptionFormat = 'full'
    }
}

What am I doing wrong? 我究竟做错了什么?

Digging into gradle, and a lot of experimenting, led me to find that replacing 挖到gradle,并进行了大量的实验,让我找到了替换

tasks.withType(Test) {
    enableAssertions = true
}

with

junitPlatformTest {
    enableAssertions = true
}

did the trick. 做了伎俩。 I don't know why. 我不知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM