Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit 5 TestSuite alternative?

Tags:

java

junit5

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?

like image 348
Bossk Avatar asked Oct 07 '17 17:10

Bossk


People also ask

Is JUnit 5 the same as Jupiter?

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.

How do I run multiple test cases in JUnit 5?

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.

What is the difference between junit4 and JUnit 5?

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.

What is Test Suite in JUnit?

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.


1 Answers

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.

like image 130
John Churchill Avatar answered Sep 18 '22 13:09

John Churchill