简体   繁体   中英

Enabling assertions in Gradle/JUnit

My project uses gradle and JUnit 5.01. The JUnit assertions are working fine. However, my regular Java assertions in the tested code itself are not firing. I would expect a failed assert to throw an AssertionError that would be caught and reported by JUnit.

I found this: How to disable assert in gradle test , and so created this 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

tasks.withType(Test) {
    enableAssertions = true
}

with

junitPlatformTest {
    enableAssertions = true
}

did the trick. I don't know why.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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