JUnit 5 Test Suites Examples

By | October 4, 2020

We can use JUnit 5 test suites to run tests spread into multiple test classes and different packages.

 JUnit 5 provides out of box two annotations: @SelectPackages and @SelectClasses to create test suites.

Additionally, we may use other annotations for filtering test packages, classes, or even test methods.

Create Test Suite with @SelectPackages

We can use @SelectPackages annotation to specify the names of packages to select when running a test suite via @RunWith(JUnitPlatform.class).

Specify Single Package

We have to pass “package_name” as a parameter to @SelectPackages annotation.

Specify Multiple Packages

We have to pass package names in the parameter as a string array (inside curly braces {}) to @SelectPackages annotation.

Point to be noted here that if we pass ‘packageA’ in @SelectPackages annotation, then test classes present in this package AND all its sub-packages will be selected for the test suite.

  Create Test Suite with @SelectClasses

We can use @SelectClasses annotation to specify the classes to select when running a test suite via @RunWith(JUnitPlatform.class).

Specify Single Class

Pass ClassName.class as parameter to @SelectClasses annotation.

Specify Multiple Classes

We can pass multiple class names in the parameter as array (inside curly braces {}) to @SelectClasses annotation.

@IncludePackages and @ExcludePackages

As we discussed above that @SelectPackages causes all it’s sub-packages as well to be scanned for test classes. If we want to include/exclude any specific package or sub-package then we may use @IncludePackages and @ExcludePackages annotations.

@IncludePackages Example

 

This will add tests from test classes in com.kkjavatutorials.packageA.packageC only i.e. ClassCTest.

@ExcludePackages Example

 

This will add tests from test classes in com.kkjavatutorials.packageA but exclude all test classes from sub-package com.kkjavatutorials.packageA.packageC i.e. ClassXTest and ClassYTest.

@IncludeClassNamePatterns and @ExcludeClassNamePatterns

Sometimes, it is not feasible to include all packages or test class names in select annotations. In that case, we may give a broader package scope and apply filtering on which test classes to be included or excluded from the suite.

To specify test class names patterns to exclude or include, you can use @IncludeClassNamePatterns and @ExcludeClassNamePatterns annotations.

@IncludeClassNamePatterns Example

Include all test classes with names ending with XTest or XTests.

 

@ExcludeClassNamePatterns Example

Exclude all test classes with name ending with XTest or XTests.

 

 We may apply more than one pattern in above annotations. In case of multiple pattern match, they are combined using OR semantics. It means that if fully qualified name of a class matches against at least one of the patterns, the class will be excluded/included from the test suite.

@IncludeTags and @ExcludeTags

In enterprise applications, we may have tagged test cases which we want to run in specific environments e.g. dev or prod. we can include or exclude tests based on these tags as well, from a test suite.

@IncludeTags Example

This test suite will run all tests tagged with prod inside package com.kkjavatutorials.packageA (and it’s sub-packages).

 

@ExcludeTags Example

This test suite will exclude all tests tagged with dev inside package com.kkjavatutorials.packageA (and it’s sub-packages).

 

Now it is very clear that there are multiple ways to create test suites in JUnit 5 and it has very strong support for filtering tests to/from test suites.

Let’s try to understand the above concept using a demo project

pom.xml

 

MyUtils.java

MyUtilsTest.java

TestingAStack.java 

TestSuiteWithExcludePackagesExample.java 

TestSuiteWithIncludePackagesExample.java

TestSuiteWithIncludeTagsExample.java

TestSuiteWithSelectPackagesAndClassesExample.java

The output of the above project:

You May Also Like:

Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5

That’s all about JUnit 5 Test Suites Examples
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.