HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

JUnit 5 Test Suites Examples

By Lokesh Gupta | Filed Under: JUnit 5

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
Its mandatory to have test class names to follow regex pattern ^.*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.

JUnit 5 Test Suite Project Structure
JUnit 5 Test Suite 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 
{
}
@SelectPackages - Single Package Example
@SelectPackages – Single Package Example

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 - Multiple Packages Example
@SelectPackages – Multiple Packages Example
Please note that if we pass ‘packageX’ in @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 
{
}
@SelectClasses - Single Class Example
@SelectClasses – Single Class Example

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 
{
}
@SelectClasses - Multiple Test Classes Example
@SelectClasses – Multiple Test Classes Example

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 
{
}
You may apply more than one pattern in above annotations. In case of multiple patterns, 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 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 !!

Sourcecode Download

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

13
Leave a Reply

This comment form is under antispam protection
7 Comment threads
6 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
10 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Gaby

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!

Vote Up0Vote Down  Reply
28 days ago
M P

Can you tell what maven modules and which version you are using for this examples?

Vote Up0Vote Down  Reply
2 months ago
Lokesh Gupta

Please refer to link.

Vote Up0Vote Down  Reply
2 months ago
Warfront1

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.

Vote Up0Vote Down  Reply
1 year ago
Lokesh Gupta

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.

Vote Up0Vote Down  Reply
1 year ago
pawa

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.

Vote Up0Vote Down  Reply
1 year ago
Lokesh Gupta

When I wrote these tutorials, eclipse did not have Junit support so they are like this. Thanks for reminding, I will update them.

Vote Up0Vote Down  Reply
1 year ago
saranya sekaran

Hello,

The test suites are executed only in Junit 4 runner. How is it possible to make it in runner junit 5

Vote Up0Vote Down  Reply
1 year ago
Some name

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!

Vote Up0Vote Down  Reply
1 year ago
Lokesh Gupta

🙂

Junit 5 is backward compitable.

Vote Up0Vote Down  Reply
1 year ago
Alice Young

No, it’s not. That’s the purpose of vintage.

Vote Up1Vote Down  Reply
1 year ago
everson mauda

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

Vote Up0Vote Down  Reply
2 years ago
Mar

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!

Vote Up0Vote Down  Reply
2 years ago

Search Tutorials

JUnit 5 Tutorial

  • JUnit 5 – Introduction
  • JUnit 5 – Test LifeCycle
  • JUnit 5 – @BeforeAll
  • JUnit 5 – @BeforeEach
  • JUnit 5 – @AfterEach
  • JUnit 5 – @AfterAll
  • JUnit 5 – @RepeatedTest
  • JUnit 5 – @Disabled
  • JUnit 5 – @Tag
  • JUnit 5 – Expected Exception
  • JUnit 5 – Assertions Examples
  • JUnit 5 – Assumptions
  • JUnit 5 – Test Suites
  • JUnit 5 – Gradle Dependency
  • JUnit 5 – Maven Dependency
  • JUnit 5 – Execute Test in Eclipse
  • JUnit 5 – Eclipse Test Templates
  • JUnit 5 vs JUnit 4

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz