eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

Partner – Diagrid – NPI (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

JUnit is one of the most popular testing frameworks for Java applications, with a powerful and flexible way to create automated unit tests. One of its features is the ability to create test suites, which allows us to group multiple tests.

In this tutorial, we’ll explore how to create test suites with JUnit. First, we’ll implement and run a simple test suite. After that, we’ll explore some configurations to include or exclude some tests.

2. Creating a Test Suite

As we know, a test suite is a collection of tests grouped together and run as a single unit. We use them to organize tests into logical groups, such as tests for a specific component or application feature. We can also easily execute tests in a particular order or run a subset of tests based on specific criteria.

JUnit 5 provides several ways to create a test suite. Before we start, we need to make sure that we include the junit-platform-suite dependency:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-suite</artifactId>
    <version>1.11.0-M1</version>
    <scope>test</scope>
</dependency>

The JUnit Platform Suite Engine is responsible for running a custom test suite using one or more test engines in JUnit. It also brings us additional APIs that we’ll use to create a test suite.

2.1. Using @Suite Annotation

The easiest way to implement a test suite is to use the @Suite class-level annotation, which is also the most recommended solution. This annotation has been available since the 1.8.0 version of the JUnit Platform.

Let’s prepare a JUnitTestSuite class:

@Suite
@SuiteDisplayName("My Test Suite")
public class JUnitTestSuite {
}

In this example, we used the @Suite annotation telling JUnit to mark this class as executable in a single unit. Moreover, we added an optional @SuiteDisplayName annotation and specified a custom title.

From now on, we can use this class to execute all tests configured in this suite in a single run by using our IDE or the maven-surefire-plugin. Note that for now, this suite doesn’t include any tests.

2.2. Using @RunWith Annotation

Alternatively, we can rewrite our test suite using the @RunWith annotation that uses the JUnit 4 Runners Model:

@RunWith(JUnitPlatform.class)
@SuiteDisplayName("My Test Suite")
public class JUnitRunWithSuite {
}

If we’re missing the JUnitPlafrom class, we need to include an additional dependency:

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-runner</artifactId>
    <version>1.11.0-M1</version>
    <scope>test</scope>
</dependency>

As a result, this test suite works similarly to our previous one created via the @Suite annotation. This solution is recommended only for developers using earlier versions of JUnit. In addition, the JUnitPlatform runner has been deprecated since 1.8.0 in favor of the @Suite support and will be removed in future releases.

3. Including and Excluding Tests

For now, our test suite doesn’t contain any tests. The JUnit Platform Suite Engine provides multiple annotations to include or exclude tests in our test suites.

We can distinguish two main groups of annotations: @Select and @Include/@Exclude.

The @Select annotations specify the resources where JUnit should look for tests. Meanwhile, the @Include/@Exclude annotations specify additional conditions to include or exclude previously found tests.

These two annotations won’t work without the @Select annotation first. We can mix all the annotations to determine exactly which tests we want to run.

Now let’s take a look at some of them by configuring our test suite.

3.1. @SelectClasses

The most common way to select tests for our test suites is to specify test classes using the @SelectClasses annotation:

@Suite
@SelectClasses({ClassOneUnitTest.class, ClassTwoUnitTest.class})
public class JUnitSelectClassesSuite {
}

The test suite now executes all @Test marked methods from both classes.

3.2. @SelectPackages

Instead of specifying the list of classes, we can use @SelectPackages to provide packages for test scanning:

@Suite
@SelectPackages({"com.baeldung.testsuite", "com.baeldung.testsuitetwo"})
public class JUnitSelectPackagesSuite {
}

Notably, this annotation also executes all classes from sub-packages.

3.3. @IncludePackages and @ExcludePackages

For now, we know how to include all classes from the packages. To further specify whether to include or exclude packages, we can use @IncludePackages and @ExcludePackages annotations, respectively:

@Suite
@SelectPackages({"com.baeldung.testsuite")
@IncludePackages("com.baeldung.testsuite.subpackage")
public class JUnitTestIncludePackagesSuite {
}

The above configuration executes all tests found in com.baeldung.testsuite.subpackage packages only, ignoring other findings.

Let’s see how we can exclude a single package:

@Suite
@SelectPackages("com.baeldung.testsuite")
@ExcludePackages("com.baeldung.testsuite.subpackage")
public class JUnitExcludePackagesSuite {
}

Now, JUnit executes all tests found in the com.baeldung.testsuite package and its sub-packages, ignoring only the classes found in the com.baeldung.testsuite.subpackage.

3.4. @IncludeClassNamePatterns and @ExcludeClassNamePatterns

If we don’t want to specify inclusion rules using packages, we can use @IncludeClassNamePatterns and @ExcludeClassNamePatterns annotations and implement regex checking for class names:

@Suite
@SelectPackages("com.baeldung.testsuite")
@IncludeClassNamePatterns("com.baeldung.testsuite.Class.*UnitTest")
@ExcludeClassNamePatterns("com.baeldung.testsuite.ClassTwoUnitTest")
public class JUnitClassNamePatternsSuite {
}

This example includes all tests found in the com.baeldung.testsuite package without its sub-packages. The class name must match the Class.*UnitTest regex pattern, like ClassOneUnitTest and ClassThreeUnitTest.

In addition, we also strictly excluded the ClassTwoUnitTest name, which will fulfill the first condition. As we know, in Java, the full class name also includes its package. This should also be taken into account when defining the patterns.

3.5. @IncludeTags and @ExcludeTags

As we know, in JUnit, we can mark classes and methods via the @Tag annotation. This’s an easy way to filter our tests with simple values. We can use the same mechanism when defining our test cases, using @IncludeTags and @ExcludeTags to run tests with specified @Tag:

@Suite
@SelectPackages("com.baeldung.testsuite")
@IncludeTags("slow")
public class JUnitTestIncludeTagsSuite {
}

This test suite will scan the com.baeldung.testsuite package and all sub-packages, running only tests annotated with the @Tag(“slow”).

Let’s now reverse the configuration:

@Suite
@SelectPackages("com.baeldung.testsuite")
@ExcludeTags("slow")
public class JUnitTestExcludeTagsSuite {
}

In this example, JUnit runs all tests without the @Tag(“slow”) annotation, including not tagged tests.

It’s worth noting that in JUnit, we can also tag a single method inside a test class. Using @IncludeTags and @ExcludeTags also allows us to include individual methods from classes, unlike the previous annotations.

3.6. @SelectMethod

Sometimes, we need to filter test methods by name in our test suites. We can use the @SelectMethod annotation to address this challenge.

Let’s see how we can select a single method:

@Suite
@SuiteDisplayName("My Test Suite")
@SelectMethod(type = ClassOneUnitTest.class, name = "whenFalse_thenFalse")
@SelectMethod("com.baeldung.testsuite.subpackage.ClassTwoUnitTest#whenFalse_thenFalse")
public class JUnitSelectMethodsSuite {
    // runs ClassOneUnitTest and ClassTwoUnitTest
}

We can select the class and method by using the type and name attributes. The type attribute specifies the class containing the test methods. The name attribute specifies the name of the test method to run within the class.

In addition, we can specify the fully qualified name of the test class and the name of the test method with ‘#’ as the delimiter.

In the above example, our test suite includes the method whenFalse_thenFalse() from the ClassOneUnitTest and ClassTwoUnitTest classes, so just the selected method from these classes will be executed when we run the suite.

4. Conclusion

JUnit provides an easy way to create automated tests, including the ability to create test suites. We can use test suites to organize our tests into logical groups, run a set of tests in a specific order, or run a subset of tests based on specific criteria.

In this article, we discussed two ways to implement a test suite, both via @Suite and @RunWith API. We then explored some additional annotations and checked how we could select tests for our test suites. Finally, we modified the list of selected tests by including and excluding them depending on various conditions.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments