Search code examples
junit4junit5junit-runner

How to run JUnit 5 suite without @RunWith annotation


For suites I am using below template:

@RunWith(JUnitPlatform.class)
@SelectPackages("com.services.configuration")
@ExcludeTags({IntegrationTags.JPA, IntegrationTags.REST})
public class UnitTestSuite {
}

@RunWith is a JUnit 4 dependency, which comes from

 compile "org.junit.platform:junit-platform-runner:1.0.2"

Is it possible to have JUnit 5 suites without the JUnit 4 dependency? So that I could remove JUnit 4 from my dependencies?

Can't it be replaced with a proper @ExtendsWith extension? I need this only for suites.


Solution

  • The JUnitPlatform runner is a means to execute tests written for JUnit Platform test engines, e.g. JUnit Jupiter tests, using JUnit 4. It is only an interim solution for tools that don't support the new JUnit Platform.

    There's an open issue for the JUnit Platform to add support for declarative suites: https://github.com/junit-team/junit5/issues/744

    For the time being, you can keep using JUnit 4 to run suites or define a custom Gradle task:

    task unitTestSuite(type: Test) {
        useJUnitPlatform {
            excludeTags "JPA", "REST"
        }
        filter {
            includeTestsMatching "com.services.configuration.*"
        }
    }