Java 在Gradle/JUnit中启用断言
我的项目使用gradle和JUnit5.01。JUnit断言工作正常。然而,我在测试代码中的常规Java断言本身并没有触发。我希望一个失败的断言抛出一个断言错误,JUnit将捕获并报告该错误 我找到了这个:,因此创建了这个build.gradle:Java 在Gradle/JUnit中启用断言,java,gradle,junit,build.gradle,junit5,Java,Gradle,Junit,Build.gradle,Junit5,我的项目使用gradle和JUnit5.01。JUnit断言工作正常。然而,我在测试代码中的常规Java断言本身并没有触发。我希望一个失败的断言抛出一个断言错误,JUnit将捕获并报告该错误 我找到了这个:,因此创建了这个build.gradle: buildscript { repositories { mavenCentral() } dependencies { classpath 'org.junit.platform:junit-
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'
}
}
我做错了什么?深入格拉德尔,做了大量的实验,让我找到了答案
tasks.withType(Test) {
enableAssertions = true
}
与
成功了。我不知道为什么。您能调试测试并确认抛出了断言错误吗?是的。我把assert false放在一个必须命中的地方。我在JUnit之外运行代码,并在启用了断言(-ea)的情况下运行代码,它命中了我的断言。但是,用JUnit运行它(“构建”gradle目标)就不行了。(它后来在我的测试中命中了一个JUnit断言。)因此我知道代码是在没有启用java断言的情况下运行的。JUnit 5是通过JUnit团队编写的插件支持的,而不是gradle(现在)的“本机”支持的。考虑到这一点,你的发现并不令人惊讶。在实现本机支持之前,将出现一些问题。作为另一个gradle用户,我希望Kotlin DSL能尽快实现这一点?
junitPlatformTest {
enableAssertions = true
}