Using JUnit 5 test suites, you can run tests spread into multiple test classes and different packages. JUnit 5 provides two annotations: @SelectPackages and @SelectClasses to create test suites. Additionally, you can use other annotations for filtering test packages, classes or even test methods.
Table of Contents Project Structure for Test classes and Suite Create Test Suite with JUnit 5 @SelectPackages Create Test Suite with JUnit 5 @SelectClasses Filtering Packages with @IncludePackages and @ExcludePackages Filtering Test Classes with @IncludeClassNamePatterns and @ExcludeClassNamePatterns Filtering Tests with @IncludeTags and @ExcludeTags
^.*Tests?$
. It means that test class names MUST end with Test or Tests. e.g. UserMgmtTests
, DeviceMgmtTest
etc.1. Project Structure for Test classes and Suite
For this example, I am using below project structure.

2. Create Test Suite with @SelectPackages
@SelectPackages
specifies the names of packages to select when running a test suite via @RunWith(JUnitPlatform.class)
.
Specify Single Package
Pass “packageName” as parameter to @SelectPackages
annotation.
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples.packageA") public class JUnit5TestSuiteExample { }

Specify Multiple Packages
Pass package names in parameter as string array (inside curly braces {}
) to @SelectPackages
annotation.
@RunWith(JUnitPlatform.class) @SelectPackages({"com.howtodoinjava.junit5.examples.packageA","com.howtodoinjava.junit5.examples.packageB"}) public class JUnit5TestSuiteExample { }

@SelectPackages
annotation, then test classes present in this package AND all it’s sub-packages will be selected for test suite.3. Create Test Suite with @SelectClasses
@SelectClasses
specifies the classes to select when running a test suite via @RunWith(JUnitPlatform.class)
.
Specify Single Class
Pass ClassName.class
as parameter to @SelectClasses
annotation.
@RunWith(JUnitPlatform.class) @SelectClasses( ClassATest.class ) public class JUnit5TestSuiteExample { }

Specify Multiple Classes
Pass class names in parameter as array (inside curly braces {}
) to @SelectClasses
annotation.
@RunWith(JUnitPlatform.class) @SelectClasses( { ClassATest.class, ClassBTest.class, ClassCTest.class } ) public class JUnit5TestSuiteExample { }

4. @IncludePackages and @ExcludePackages
As we learn that @SelectPackages
causes all it’s sub-packages as well to be scanned for test classes. If you want to exclude any specific sub-package, or include any package then you may use @IncludePackages and @ExcludePackages annotations.
@IncludePackages Example
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @IncludePackages("com.howtodoinjava.junit5.examples.packageC") public class JUnit5TestSuiteExample { }
This will add tests from test classes in com.howtodoinjava.junit5.examples.packageC
only i.e. ClassCTest
.
@ExcludePackages Example
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @ExcludePackages("com.howtodoinjava.junit5.examples.packageC") public class JUnit5TestSuiteExample { }
This will add tests from test classes in com.howtodoinjava.junit5.examples
but exclude all test classes from sub-package com.howtodoinjava.junit5.examples.packageC
i.e. ClassATest
and ClassBTest
.
5. @IncludeClassNamePatterns and @ExcludeClassNamePatterns
Many times it is not feasible to include all packages or test class names in select annotations. In that case, you may give a broader package scope and appy filtering on which test classes to be included or excluded from 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 name ending with ATest
or ATests
.
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @IncludeClassNamePatterns({"^.*ATests?$"}) public class JUnit5TestSuiteExample { }
@ExcludeClassNamePatterns Example
Exclude all test classes with name ending with ATest
or ATests
.
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @ExcludeClassNamePatterns({"^.*ATests?$"}) public class JUnit5TestSuiteExample { }
OR
semantics. It means that if fully qualified name of a class matches against at least one of the patterns, the class will be included/excluded from the test suite.6. @IncludeTags and @ExcludeTags
In enterprise applications, you may have tagged test cases which you want to run in specific environments e.g. development or production. You 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 production
inside package com.howtodoinjava.junit5.examples
(and it’s sub-packages).
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @IncludeTags("production") public class JUnit5TestSuiteExample { }
@ExcludeTags Example
This test suite will exclude all tests tagged with development
inside package com.howtodoinjava.junit5.examples
(and it’s sub-packages).
@RunWith(JUnitPlatform.class) @SelectPackages("com.howtodoinjava.junit5.examples") @ExcludeTags("development") public class JUnit5TestSuiteExample { }
Clearly, there are multiple ways to create test suites in JUnit 5 and it has a strong support for filtering tests to/from test suites.
Drop me your questions in comments section.
Happy Learning !!
Hello,
Thanks for your great sharing!
Q1: If I have multiple test suites class(ex: one for regression Test, another for Smoke test), how would I go about executing specific one from a script ?
Q2 In Jenkins Job, how to configure certain(different) suites to run different cases?
Thanks!
Can you tell what maven modules and which version you are using for this examples?
Please refer to link.
This is using the JUnit 4 runner in order to execute Junit 5 Tests. The title is extremely misleading. It would suggest that you are able to run suites without JUnit4, solely using Junit5.
When these tutorials were written, IDEs didn’t have junit 5 support. But you can run same test code with Junit 5 runner, without any modification.
Did you ever run it with JUnit5?
Because print screens are of JUnit4 runner.
So would be great if you change the header of this tutorial or provide an example with JUnit5.
When I wrote these tutorials, eclipse did not have Junit support so they are like this. Thanks for reminding, I will update them.
Hello,
The test suites are executed only in Junit 4 runner. How is it possible to make it in runner junit 5
I think he didn’t even try his own code. Just copy pasted it from another source code. This code won’t work in JUnit5!
🙂
Junit 5 is backward compitable.
No, it’s not. That’s the purpose of vintage.
Hello, how are you?
I studing JUnit5 now and in my examples, the annotations @SelectClasses and @SelectPackages are only for JUnit Vintage, with is the same of JUnit4.
If you want, put in the article’s begining an observtion to no make some confusion to your readers.
Thanks
If I have multiple test suites, how would I go about executing specific ones from a script or from a gradle task? In one job/task, I may only want one of my test suites to run, while in others, I may want different ones to run or want to exclude certain suites. Thanks!