How to run JUnit 5 suite without @RunWith annotation
Asked Answered
E

2

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.

Epigraph answered 3/4, 2018 at 18:35 Comment(0)
R
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.*"
    }
}
Rimskykorsakov answered 3/4, 2018 at 19:39 Comment(1)
Thank you for your answer.So now we need to wait. Because this dependency is for back-compatibility.Epigraph
P
3

I gave a similar answer here: https://mcmap.net/q/1776193/-junit5-test-suite-with-kotlin

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

Picoline answered 23/8, 2021 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.