JUnit 5 Test Suite Tutorial

A test suite is a collection of test cases that are run together as a single unit. It helps to organize and manage the execution of multiple test cases. Test suites can be used to group related tests or to test different aspects of a complex system. They can also be used to run tests in a specific order or to exclude certain tests from the execution.

To learn more, check out the Testing Java Code tutorials page.

Why use Test Suite in JUnit 5?

Test Suite in JUnit 5 can help you organize and manage your tests. Instead of running each test case or test class individually, you can run all the tests in a suite at once. This can save you time and make your tests more maintainable.

@Suite Annotation

JUnit 5 provides a @Suite annotation to create a test suite. This annotation allows you to group multiple test classes or packages into a single suite. You can use the @Suite annotation to include or exclude specific tests or packages from the suite.

Notice how the @Suite annotation is used in the code example below:

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.ExcludePackages;
import org.junit.platform.suite.api.IncludePackages;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@RunWith(JUnitPlatform.class)
@Suite
@SelectClasses({ FirstClassTest.class, SecondClassTest.class, ThirdClassTest.class })
@IncludePackages("com.example.package1")
@ExcludePackages("com.example.package2")
public class MyTestSuite {
}

In this example, we’re using the @Suite annotation to specify that this class is a test suite.

Creating Simple Test classes

To create a test class in JUnit 5, you need to create a new Java class and add the @Test annotation to each method that represents a test. Here is an example of a test class named “FirstClassTest” in the package “com.example.demo.package1”:

Test class 1: Create a new test class with a single test method

package com.example.demo.package1;

import org.junit.jupiter.api.Test;

public class FirstClassTest {

    @Test
    public void test1() {
        System.out.println("Running test 1");
    }
    
}

In this example, we have added the @Test annotation to the method called “test1”. This method will run when we execute the test suite.

Test class 2: Create a second test class with a single test method

Here is an example of a second test class named “SecondClassTest” in the package “com.example.demo.package2”:

package com.example.demo.package2;

import org.junit.jupiter.api.Test;

public class SecondClassTest {

    @Test
    public void test2() {
        System.out.println("Running test 2");
    }
}

Creating a Test Suite

To create a Test Suite in JUnit 5, we can use the @Suite annotation. The @Suite annotation is used to group multiple test classes into one test suite. To create a test suite, create a new class and annotate it with @Suite annotation. Here is an example of a Test Suite:

import org.junit.platform.runner.JUnitPlatform;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

import first.class.package.FirstClassTest;
import second.class.package.SecondClassTest;

@Suite
@SelectClasses({
    FirstClassTest.class,
    SecondClassTest.class
})
public class TestSuite {

}

In this example, we have created a Test Suite class named “TestSuite”. We have included the @Suite annotation to specify that this class is a test suite. The @SelectClasses annotation is used to specify the test classes that should be included in this test suite.

How to include class into a test suite?

To include a test class in a test suite in JUnit 5, we can use the @SelectClasses annotation. Here is an example:

@Suite
@SelectClasses({
    FirstClassTest.class
})
public class TestSuite {

}

In this example, we have included the @SelectClasses annotation to specify that we want to include only the “FirstClassTest” class in the test suite.

How to include a package into a test suite?

To include all the test classes in a package in a test suite in JUnit 5, we can use the @SelectPackages annotation. Here is an example:

@Suite
@SelectPackages("com.example.demo.package1")
public class TestSuite {

}

In this example, we have included the @SelectPackages annotation to specify that we want to include all the test classes in the “com.example.demo.package1” package in the test suite.

How to exclude class from a test suite in JUnit 5?

To exclude a test class from a test suite in JUnit 5, we can use the @ExcludeClasses annotation. Here is an example:

@Suite
@ExcludeClasses({
    SecondClassTest.class
})
public class TestSuite {

}

In this example, we have included the @ExcludeClasses annotation to specify that we want to exclude the “SecondClassTest” class from the test suite.

How to exclude package from a test suite?

To exclude all the test classes in a package from a test suite in JUnit 5, we can use the @ExcludePackages annotation. Here is an example:

@RunWith(JUnitPlatform.class)
@Suite
@ExcludePackages("com.example.demo.package2")
public class TestSuite {

}

In this example, we have included the @ExcludePackages annotation to specify that we want to exclude all the test classes in the “com.example.demo.package2” package from the test suite.

How to Run a Test Suite in JUnit 5?

To run a Test Suite in JUnit 5, we can simply run the test suite class. In Eclipse, we can right-click on the test suite class and select “Run As” > “JUnit Test”. Alternatively, we can run the test suite from the command line using the following command:

mvn test

This will run all the tests in the test suite. Make sure you have the following configuration in pom.xml file of your maven-based project.

Test Suite Maven Dependency

<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite</artifactId>
    <scope>test</scope>
</dependency>

Build Plugin

<build>
       <plugins>

           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-surefire-plugin</artifactId>
               <version>3.0.0-M9</version>
               <configuration>
                   <test>com.example.demo.MyTestSuiteTest</test>
               </configuration>
           </plugin>
       </plugins>
   </build>

What is the difference between JUnit test suite and test case?

In JUnit, a test case is a class that contains one or more test methods. Each test method is a unit of testing, and it tests a specific functionality or behavior of the code being tested.

On the other hand, a test suite is a collection of test cases. A test suite allows you to group related test cases together and run them all at once. This can be useful if you want to test a larger functionality that involves multiple test cases or if you want to run all your tests at once.

So, the main difference between a JUnit test case and a test suite is that a test case is a single unit of testing, while a test suite is a collection of test cases.

To illustrate the difference, let’s say you have a class called “Calculator” that contains methods for addition, subtraction, multiplication, and division. You can create a JUnit test case for each method, where each test case tests the functionality of a single method. Then, you can group all the test cases into a test suite and run them all at once to ensure that the entire “Calculator” class works as expected.

In summary, a JUnit test case tests a single functionality or behavior, while a test suite groups multiple test cases together for convenience and organization.

Can I include both test cases and test classes in a JUnit Test Suite?

es, you can include both test cases and test classes in a JUnit Test Suite. In fact, this is one of the benefits of using a test suite – it allows you to group related test cases and test classes together in a single suite.

To include test cases in a JUnit Test Suite, you can use the @IncludeTags and @ExcludeTags annotations. For example, you could add the following annotation to a test method:

@Test
@Tag("calculator")
public void testAddition() {
    // Test code goes here
}

Then, in your test suite class, you can include the test method by using the @SelectClasses or @SelectPackages annotations and filtering by the “calculator” tag:

@Suite
@SelectPackages("com.example")
@IncludeTags("calculator")
public class CalculatorTestSuite {
    // Test cases and classes go here
}

Alternatively, you can include test classes in your test suite by using the @SelectClasses and @SelectPackages annotations as it was already demonstrated in this tutorial earlier.

Can I run a Test Suite on multiple threads in JUnit 5?

Yes, you can run a Test Suite on multiple threads in JUnit 5. This feature is called “parallel test execution”, and it allows you to run your tests on multiple threads, which can significantly reduce the time it takes to run your tests.

To enable parallel test execution in JUnit 5, you can use the @Execution annotation with the CONCURRENT value. For example, you could annotate your test suite class like this:

@Suite
@SelectPackages("com.example")
@Execution(CONCURRENT)
public class MyTestSuite {
    // Test cases and classes go here
}

When you run your test suite, JUnit 5 will automatically create multiple threads and distribute the test cases across them. By default, JUnit 5 will use the same number of threads as the number of available processors on your machine, but you can customize this by using the @Execution(CONCURRENT) annotation with a different value, such as @Execution(CONCURRENT_2) to use two threads, or @Execution(CONCURRENT_UNLIMITED) to use as many threads as possible.

Note that when running tests in parallel, you should make sure that your tests are thread-safe, and that they do not depend on the order in which they are run. You should also be aware that running tests in parallel can increase the risk of race conditions and other concurrency issues, so you should test your code thoroughly to ensure that it is robust under concurrent execution.

Conclusion

In this tutorial, we explored the benefits of using test suites in JUnit 5 and learned about the @Suite annotation, which serves as the foundation for creating test suites. We discussed the process of creating simple test classes and demonstrated how to include them in a test suite. Additionally, we explored how to include entire packages or exclude specific classes or packages from a test suite, offering flexibility in test configuration.

To execute a test suite, we examined the necessary steps, including adding the Test Suite Maven dependency and configuring the build plugin. By following these guidelines, developers can seamlessly run their test suites within their Java projects. Don’t forget to visit the Testing Java Code page for a variety of informative tutorials.