在 Gradle/JUnit 中启用断言

Enabling assertions in Gradle/JUnit

我的项目使用 gradle 和 JUnit 5.01。 JUnit 断言工作正常。但是,我在测试代码本身中的常规 Java 断言没有触发。我希望失败的断言会抛出一个 AssertionError,它会被 JUnit 捕获并报告。

我发现了这个:How to disable assert in gradle test,所以创建了这个 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'
    }
}

我做错了什么?

深入研究 gradle,并进行了大量试验,让我发现替换

tasks.withType(Test) {
    enableAssertions = true
}

junitPlatformTest {
    enableAssertions = true
}

成功了。不知道为什么。