Getting started with JUnit 5 TestSuite

A JUnit 5 TestSuite can be used to to run multiple tests together. They allow to aggregate multiple test classes. JUnit 5 provides the following annotations:

  • @SelectPackages – used to specify the names of packages for the test suite
  • @SelectClasses – used to specify the classes for the test suite. They can be located in different packages.

Here is an example which uses both annotation to select two Test Classes and a package:

package com.example;

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.IncludeTags;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.runner.RunWith;


@RunWith(JUnitPlatform.class)
@SelectClasses( { AppTest.class, AppTest2.class } )
@SelectPackages("com.example")
public class TestSuiteDemo {
}

Include the following dependency to run the Testsuite class:

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <junit-platform.version>1.2.0</junit-platform.version>
    <junit-jupiter.version>5.2.0</junit-jupiter.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.junit.platform</groupId>
      <artifactId>junit-platform-runner</artifactId>
      <version>${junit-platform.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit-jupiter.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-params</artifactId>
      <version>${junit-jupiter.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit-jupiter.version}</version>
      <scope>test</scope>
    </dependency>

  </dependencies>