6

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.

Sam Brannen
  • 29,611
  • 5
  • 104
  • 136
Beri
  • 11,470
  • 4
  • 35
  • 57

2 Answers2

4

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.*"
    }
}
Marc Philipp
  • 1,848
  • 12
  • 25
  • Thank you for your answer.So now we need to wait. Because this dependency is for back-compatibility. – Beri Apr 04 '18 at 05:36
3

I gave a similar answer here: https://stackoverflow.com/a/68898733/2330808

You can now run Suites purely with jUnit 5 engines:

import org.junit.platform.suite.api.SelectClasses
import org.junit.platform.suite.api.Suite

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

You'll have to import the junit-platform-suite-engine:

https://search.maven.org/artifact/org.junit.platform/junit-platform-suite-engine

eg compile "org.junit.platform:junit-platform-suite-engine:1.8.0-RC1"

There's some docs here:

https://junit.org/junit5/docs/snapshot/user-guide/#launcher-api-engines-custom https://junit.org/junit5/docs/snapshot/api/org.junit.platform.suite.engine/org/junit/platform/suite/engine/package-summary.html

Here's an official example:

https://github.com/junit-team/junit5/blob/main/documentation/src/test/java/example/SuiteDemo.java

Nico
  • 169
  • 6