Junit 5 test suites not running

I’m migrating my junit test cases from 4 to 5.
In Junit 4 I have test suites.
The new Junit 5 “test suites” runs fine from Eclipse but not from commandline.

Windows 10, gradle 5.5.1, Java 8

Build.gradle snippet

dependencies {
    implementation 'junit:junit:4.12'
    implementation 'org.junit.jupiter:junit-jupiter-api:5.5.0'
    implementation 'com.sun.xml.parsers:jaxp-ri:1.4.5'
    implementation 'org.apache.commons:commons-csv:1.6'

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.5.0'
    testImplementation 'org.junit.platform:junit-platform-suite-api:1.5.0'
    testImplementation 'org.junit.platform:junit-platform-runner:1.5.0'

    testRuntime 'org.junit.jupiter:junit-jupiter-engine:5.5.0'
}

test {
    useJUnitPlatform()
    include '**/AllTests.class'
}

The Junit suite snippet:

package dk.junit.run;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.runner.RunWith;

import dk.progress.JobTest;
import dk.progress.TaskTest;

@RunWith(JUnitPlatform.class)
@SelectClasses({
    TaskTest.class,
    JobTest.class
})
public class AllTests {
}

Gradle runs zero test.

Is there a way that I can the the test suites in Junit 5 and run it from gradle?

1 Like

I think the issue is with your include '**/AllTests.class', which doesn’t include the selected classes TaskTest and JobTest.

1 Like

If I run the AllTests.class in Eclipse(2019.6) then TaskTest and JobTest are also run.
But that does not work with Gradle.

If I remove the include statement in Gradle then TaskTest and JobTest are run. AllTests is not run properly because is doesn’t have a method annotated with @Test.

But I don’t understand why the @SelectClasses annotation doesn’t work with Gradle.

Check to make sure that you’re using the org.junit.jupiter.api package for the @Test annotation. Junit didn’t switch the actual annotation name going from 4 -> 5 like they did for most of the rest. It’s very easy to accidentally use the wrong import.

The @Test is not the problem.

The problem is that AllTests is a Junit 4 construction. And it can’t be run with Junit 5.
Eclipse is smart enough to figure this out and use the right “runner”.

The solution is to delete the AllTests.java and remove the include statement in the gradle build file.

I just found out that I can right-click on a package or the test source folder and select Run As Junit Test to get a similarly behaviour as my AllTests.

1 Like

I had the exact same problem except it was with the @test annotation from junit 4 and intellji running it when Gradle was looking for junit 5 annotations and silently passing everything by executing none of the tests.

i am trying to create a test suite for Unit test of android application, but it always fails with different kinds of configuration error, one among that is as follows,

Error: Test events were not received android
FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:appModules:factList:testDebugUnitTest’.

No tests found for given includes: [reprator.wipro.factlist.FactListTestSuite]
(filter.includeTestsMatching)
RepoDetails: GitHub - TheReprator/Wipro at junit5

Branch: junit5

TestSuite Class: Wipro/FactListTestSuite.kt at junit5 · TheReprator/Wipro · GitHub

I had tried many efforts and references, some of them are as follows,

My whole code works perfectly with junit4 in master branch.
Stackoverflow: Junit5 testSuite with SelectClasses not working in android unit test - Stack Overflow

Please assist.