Mridang Agarwalla Mridang Agarwalla - 1 year ago 78
Java Question

How can I use a custom runner when using categories in Junit?

I have a bunch of JUnit tests that extend my base test class called

which in turn extends
. Some of my tests have a

class is annotated with the following annotation

I've set up a Gradle task that is expected to run only
. Here's my Gradle task:

task integrationTests(type: Test) {
minHeapSize = "768m"
maxHeapSize = "1024m"
testLogging {
events "passed", "skipped", "failed"
outputs.upToDateWhen {false}
reports.junitXml.destination = "$buildDir/test-result"
useJUnit {
includeCategories 'testutils.SlowTests'

When I run the task, my tests aren't run. I've pinpointed this issue to be related to the custom runner
on the
. How can I set up my Gradle or test structure so that I can use a custom runner while using the

Answer Source

The solution to this turned out to smaller and trickier than I thought. Gradle was using my custom test runner and correctly invoking the filter method. However, my runner reloads all test classes through its own classloader for Javaassist enhancements.

This lead to the issue that SlowTest annotation was loaded through the Gradle classloader but when passed to my custom runner, the runner checked if the class was annotated with that annotation. This check never resolved correctly as the equality of the SlowTest annotation loaded through two different classloaders was different.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download