I'm in the process of migrating from JUnit 4 to 5 and decided to rewrite all old @Tests to the new org.junit.jupiter.api.Test.
My goal is to drop the old junit4 dependency completely, only keeping these dependencies:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.0.1</version>
<scope>test</scope>
</dependency>
So far I've rewritten everything except my TestSuites, which I use to cluster all Tests into 4 separate suites, callable by the surefire plugin.
It seems that JUnit 5 has no declarative suite support at all, but does have the @Tag annotation.
Question:
How can I create some kind of testsuite-alternative with only JUnit 5 stuff, callable with the maven-surefire-plugin ( <includes>
) AND runnable IntelliJ?
It's important, however, to understand that the two terms are not the same. JUnit Jupiter is the API for writing tests using JUnit version 5. JUnit 5 is the project name (and version) that includes the separation of concerns reflected in all three major modules: JUnit Jupiter, JUnit Platform, and JUnit Vintage.
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.
Only one test runner can execute tests at a time in JUnit 4 (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously. JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of the Java 8 features.
Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.
Despite the rich junit-platform-suite-api, it's currently (June 2018) not possible, because native test suite support is not in the platform. Instead, you must use the junit-platform-runner which depends on junit-4.12.jar to identify and run suites.
@RunWith(JUnitPlatform.class)
@SelectClasses({Dog.class, Cat.class})
public class MyTestSuite {
}
However, native support might be coming in 1.3 version of the platform, according to this github issue.
I deleted all of my test suites to avoid having junit 4 in my projects at all. I will happily create them again using the new functionality when it's available.
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