JUnit Listener Configuration In Gradle

自作多情 提交于 2019-12-02 01:16:39

I’m afraid, there is currently no support for JUnit RunListeners in Gradle. There is only an open ticket requesting that feature: https://github.com/gradle/gradle/issues/1330

As someone has mentioned in the comments on that ticket, “the primary issue […] is the absence of TestDescriptor.getAnnotations()” in Gradle; otherwise you might have been able to rewrite your RunListener as a Gradle TestListener. So unless I’ve missed something when skimming through the ticket, it seems that you are mostly out of luck at the moment :-(

The JUnit Foundation library enables you to declare your listeners in a service provider configuration file, which are then attached automatically - regardless of execution environment. Details can be found here.

Gradle Configuration for JUnit Foundation

// build.gradle
...
apply plugin: 'maven'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
    mavenLocal()
    mavenCentral()
    ...
}
dependencies {
    ...
    compile 'com.nordstrom.tools:junit-foundation:9.1.1'
}
ext {
    junitFoundation = configurations.compile.resolvedConfiguration.resolvedArtifacts.find { it.name == 'junit-foundation' }
}
test.doFirst {
    jvmArgs "-javaagent:${junitFoundation.file}"
}
test {
//  debug true
    // not required, but definitely useful
    testLogging.showStandardStreams = true
}

ServiceLoader provider configuration file

# src/main/resources/META-INF/services/org.junit.runner.notification.RunListener
com.example.MyRunListener

With this configuration, the listener implemented by MyRunListener will be automatically attached to the RunNotifier supplied to the run() method of JUnit runners. This feature eliminates behavioral differences between the various test execution environments like Maven, Gradle, and native IDE test runners.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!