Mridang Agarwalla Mridang Agarwalla - 1 month ago 8
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

BaseTest
which in turn extends
Assert
. Some of my tests have a
@Category(SlowTests.class)
annotation.

My
BaseTest
class is annotated with the following annotation
@RunWith(MyJUnitRunner.class)
.

I've set up a Gradle task that is expected to run only
SlowTests
. 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
MyJUnitRunner
on the
BaseTest
. How can I set up my Gradle or test structure so that I can use a custom runner while using the
Suite
.

Answer

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.

Comments