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.
Creating Test Suites Creating suites is easy. Just add the @Suite annotation of a class and start including or excluding the test classes and methods into the suite. When we want to run the suite, simply run it as a normal JUnit test class and it will execute all the included tests in the suite.
If a JUnit class or its parent class is annotated with @RunWith, JUnit framework invokes the specified class as a test runner instead of running the default runner.
The annotation is used to configure a unit test that required Spring's dependency injection. In order for the unit test to run a batch job, the framework must load the job's ApplicationContext. Two annotations are used to trigger this: @RunWith(SpringJUnit4ClassRunner.
When a class is annotated with @RunWith or extends a class annotated with @RunWith , JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development.
JUnit 5 test suites are written with @Suite annotation. Suites help us run the tests spread into multiple classes and packages. We can use Include** and Exclude** annotations (discussed later in this tutorial) for filtering test packages, test classes or even test methods.
The "equivalent" in JUnit 5 is ExtendWith, which expects a JUnit 5 extension class as argument. You'll need to add a dependency on JUnit 4, write the test with the JUnit 4 API, and use the vintage engine of JUnit 4 if there is no CacioExtension class. Does this answer your question? Equivalent for @RunWith (JUnitPlatform.class) for JUnit5
In Junit, test suite allows us to aggregate all test cases from multiple classes in one place and run it together. To run the suite test, you need to annotate a class using below-mentioned annotations: With above annotations, all the test classes in the suite will start executing one by one. Steps to create Test Suite and Test Runner.
@RunWith (JUnitPlatform.class) has been deprecated in favor of @Suite annotation and will be removed in JUnit Platform 2.0. 1. Project Structure and Maven Dependency 2. Creating Test Suites 2.1. @Suite 3.2. @SuiteDisplayName 3. Including and Excluding Tests 3.1.
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.*"
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With