Run the Right Subset of a REST Assured Suite With Tags and Maven Profiles
Organise a large API test suite with JUnit tags and a composed @SmokeTest annotation, and run the right subset with Maven profiles that set Surefire's groups and excludedGroups, or with a tag expression on the command line.
A test suite grows. At first, running everything on every change is fine. Then it takes twenty minutes, half of it Keycloak and other containers, and not every situation needs all of it. After a deployment, you want to know in a minute whether the service is up. Before a release, you want every security check. On a machine without Docker, you want everything that can run there. One suite, several purposes. In this guide, you’ll tag tests by what they’re for, and run the right subset with Maven profiles and tag expressions.
Level: Advanced · Time: 20 minutes · Part of: Module 12: Real Infrastructure with Testcontainers, section 3
What You’ll Build
You’ll build a composed @SmokeTest annotation, a test class called TaggedSuiteTest with two smoke tests and one regression test, and three Maven profiles. Then you’ll run the module’s 16 tests in five different subsets.
Each subset is one command:
| Command | Runs | Tests |
|---|---|---|
mvn test -pl module-12 | Everything | 16 |
mvn test -pl module-12 -Psmoke | @Tag("smoke") only | 2 |
mvn test -pl module-12 -Psecurity | @Tag("security") only | 5 |
mvn test -pl module-12 -Pno-docker | Everything except keycloak | 11 |
mvn test -pl module-12 -Dgroups="smoke \| security" | Smoke or security | 7 |
When you’re done, the smoke run finishes in about a second and a half:
1
2
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
In this guide, you will:
- Tag the tests.
- Select tests with profiles.
- Select tests with an expression.
What You Need
- About 20 minutes
- JDK 26 or later
- Maven 3.9 or later
- Docker, for the runs that include the Keycloak tests
- An IDE (IntelliJ IDEA, VS Code or Eclipse)
- Knowledge: you know the module’s Keycloak and parallel tests, from Test With Real OAuth 2.0 Tokens From Keycloak and Testcontainers and Run REST Assured Tests in Parallel With JUnit, and what a Maven profile is.
How to Complete This Guide
You can work through this guide in one of two ways:
- Build it step by step: create
SmokeTestin the module’ssupportpackage andTaggedSuiteTestin a newsection_03package, and follow every heading below. - Skip ahead: open the finished class (
TaggedSuiteTest.java) and read along. You can run the commands at any time with those in Run the Tests.
Get the code: if you haven’t cloned the examples repository yet, follow the steps in Test With Real OAuth 2.0 Tokens From Keycloak and Testcontainers.
Out of scope: this guide doesn’t cover configuring a CI server; the commands are the same in any pipeline.
Understand Tags and Profiles
The problem. A large suite serves several purposes, and each needs a different subset: a quick check after a deployment, the security checks before a release, everything that runs without Docker on every commit. Selecting tests by class or package breaks as soon as a class holds tests of more than one kind.
The concept. Three pieces work together:
- A tag is a label on a test class or method,
@Tag("smoke"). A test can have several, and a class’s tags apply to all its methods. - Surefire’s
groupsandexcludedGroupsselect tests by tag: only tests with these tags, or every test except those. Both accept JUnit tag expressions:|for or,&for and,!for not, and parentheses. - A Maven profile is a named set of build settings, switched on with
-P<name>. Here, each profile setsgroupsorexcludedGroups.
When to use each. Give the subsets a team uses every day a profile, so the command is short and the same in every pipeline. Use a tag expression on the command line for everything else.
Tag tests by what they’re for, and let the build choose by purpose.
Meet the Suite Under Test
The module has 16 tests in three classes, tagged like this:
| Tag | On | Meaning |
|---|---|---|
smoke | Two tests in TaggedSuiteTest | Fast checks that the service is up and basically works |
regression | One test in TaggedSuiteTest | A slower, more detailed check |
reference-data | TaggedSuiteTest | Tests of the reference data API |
keycloak, security | KeycloakTokensTest, five tests | Needs Docker; checks authentication and authorisation |
| (none) | ParallelExecutionTest, eight tests | Reference data reads in parallel |
The course’s parent POM also sets excludedGroups to expected-failure-tests, which excludes the deliberately failing tests in Module 1 by default.
The things you’ll check:
- The number of tests each command runs.
- How long each subset takes, and whether it needs Docker.
Tag the Tests
Write a Composed Smoke Annotation
Goal: make a smoke test one annotation, so the tag can’t be misspelled.
Create this annotation in the support package:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.javanibble.module_12.support;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
/** A test that belongs in the smoke suite: fast, and checks that the service is up and basically works. */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@Tag("smoke")
public @interface SmokeTest {
}
What’s happening:
@SmokeTestcarries@Testand@Tag("smoke"), so JUnit treats a method with it as a test with thesmoketag.- The tag name is written once, here, instead of in every smoke test.
Checkpoint: the annotation compiles.
Tag the Test Class
Goal: write two smoke tests and one regression test, all tagged reference-data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.javanibble.module_12.section_03;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.lessThan;
import com.javanibble.module_12.support.SmokeTest;
import com.javanibble.reference.ReferenceDataApi;
import io.restassured.specification.RequestSpecification;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Tag("reference-data")
class TaggedSuiteTest {
private static ReferenceDataApi api;
@BeforeAll
static void startApi() {
api = ReferenceDataApi.start();
}
@AfterAll
static void stopApi() {
api.close();
}
private static RequestSpecification referenceData() {
return given().baseUri(api.baseUri()).port(api.port()).basePath("/api/v1");
}
@SmokeTest
void theServiceAnswers() {
referenceData()
.when()
.get("/currencies")
.then()
.statusCode(200)
.time(lessThan(2L), TimeUnit.SECONDS);
}
@SmokeTest
void theMainCurrencyIsThere() {
referenceData().when().get("/currencies/{code}", "EUR").then().body("name", equalTo("Euro"));
}
@Test
@Tag("regression")
void pastHolidaysAreKept() {
referenceData().when().get("/holidays/{country}", "NL").then().body("dates[0]", equalTo("2026-01-01"));
}
}
What’s happening:
@Tag("reference-data")on the class applies to all three tests.- The two smoke tests answer one question each: does the service answer, within a generous two seconds, and is its most important data there?
- The detailed check, which holiday comes first, is a regression test, for the full suite.
Checkpoint: run TaggedSuiteTest from your IDE. All three tests pass; the IDE runs every test, whatever its tags.
Keep smoke tests fast and few. A smoke suite that needs Keycloak, or checks every field, isn’t a smoke suite: it runs after every deployment, and its job is to answer “is it up?” in a minute.
Select Tests With Profiles
Define the Profiles
Goal: give the everyday subsets a name.
The module’s pom.xml defines three profiles:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<profiles>
<profile>
<id>smoke</id>
<properties>
<groups>smoke</groups>
</properties>
</profile>
<profile>
<id>security</id>
<properties>
<groups>security</groups>
</properties>
</profile>
<profile>
<id>no-docker</id>
<properties>
<excludedGroups>keycloak,expected-failure-tests</excludedGroups>
</properties>
</profile>
</profiles>
What’s happening:
- Surefire reads the
groupsandexcludedGroupsproperties, so a profile only has to set them. smokeandsecurityselect by tag;no-dockerexcludes thekeycloaktag.no-dockerreplaces the parent’sexcludedGroups, so it listsexpected-failure-testsagain. In this module there are no such tests, but the profile stays correct if some are added.
Checkpoint: the profiles are in the module’s pom.xml; the next step runs them.
Run Each Profile
Goal: see which tests each profile selects, and what it costs.
Run each command from the repository root, with Docker running:
1
2
3
4
mvn test -pl module-12
mvn test -pl module-12 -Psmoke
mvn test -pl module-12 -Psecurity
mvn test -pl module-12 -Pno-docker
The four runs reported:
| Command | Tests run | Total time |
|---|---|---|
| (no profile) | 16 | 16.1 s |
-Psmoke | 2 | 1.5 s |
-Psecurity | 5 | 13.0 s |
-Pno-docker | 11 | 1.8 s |
What’s happening:
- The smoke run needs no Docker, and finishes in a second and a half, including Maven.
- The security run is the five Keycloak tests, and almost all of its time is Keycloak starting.
- The
no-dockerrun is everything else: the parallel and tagged reference data tests.
Checkpoint: your counts match the table.
There are two ways to handle a machine without Docker, and they look different in the report.
disabledWithoutDocker = trueon the Keycloak class reports its tests as skipped;-Pno-dockerdoesn’t select them at all, and they still don’t run when Docker is there. Use the annotation as a safety net on laptops, and the profile on CI agents that never have Docker.
Select Tests With an Expression
Combine Tags on the Command Line
Goal: run a subset no profile names.
Run this command from the repository root:
1
mvn test -pl module-12 -Dgroups="smoke | security"
You should see a result similar to the following, after Keycloak starts:
1
2
[INFO] Tests run: 7, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
What’s happening:
-Dgroups=..sets the same property a profile does, for one run.smoke | securityselects the two smoke tests and the five security tests.- The quotes matter: without them, the shell treats
|as a pipe, and runsmvn test -pl module-12 -Dgroups=smokewith its output piped into a command calledsecurity.
Checkpoint: seven tests run and pass.
Run the Tests
The commands for this section are the ones above. A pipeline can use them like this:
| Stage | Command | Why |
|---|---|---|
| Every commit | mvn test -pl module-12 -Pno-docker | Fast feedback without containers |
| Nightly or before merge | mvn test -pl module-12 | The full suite, with real infrastructure |
| After each deployment | mvn test -pl module-12 -Psmoke | Is it up, in a minute? |
| Before a release | mvn test -pl module-12 -Psecurity | Every authentication and authorisation check |
To run only this section’s class, use mvn test -pl module-12 -Dtest=TaggedSuiteTest:
1
2
3
[INFO] Running com.javanibble.module_12.section_03.TaggedSuiteTest
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.072 s -- in com.javanibble.module_12.section_03.TaggedSuiteTest
[INFO] BUILD SUCCESS
Make a Test Fail
Tags fail differently from tests: a wrong tag doesn’t fail anything, it makes a test disappear. In TaggedSuiteTest, replace @SmokeTest on theMainCurrencyIsThere with @Test and @Tag("smok"), and run mvn test -pl module-12 -Psmoke. You should see a result similar to the following:
1
2
3
4
[INFO] Running com.javanibble.module_12.section_03.TaggedSuiteTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.397 s -- in com.javanibble.module_12.section_03.TaggedSuiteTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
The build is green, and the smoke suite is one test short. Nothing warns you. Misspell the tag in the command instead, -Dgroups=smok, and Maven runs zero tests, and still reports BUILD SUCCESS. This is why @SmokeTest exists: the tag is written in one place.
Put @SmokeTest back and run the smoke profile once more to confirm it runs two tests.
Add
-DfailIfNoTests=trueto a pipeline’s tag runs. With it,-Dgroups=smokfailed the build withNo tests were executed!instead of passing with zero tests.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A test is missing from a subset | Its tag is misspelled or missing | Use a composed annotation such as @SmokeTest |
Tests run: 0 and BUILD SUCCESS | The tag in groups matches nothing | Check the spelling, and add -DfailIfNoTests=true |
A shell error about a command called security | An unquoted tag expression | Quote it: -Dgroups="smoke \| security" |
| Deliberately failing tests run | A profile replaced the parent’s excludedGroups | List expected-failure-tests in the profile too |
| The Keycloak tests are skipped | Docker isn’t running | Start Docker, or use -Pno-docker to leave them out |
Try It Yourself
Exercise 1: Run Only the Regression Tests
Run only the tests tagged regression, without adding a profile, and check that exactly one test runs.
Hint
-Dgroups=.. takes a single tag as well as an expression.
Solution
1
mvn test -pl module-12 -Dgroups=regression
1
2
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
The one test is pastHolidaysAreKept. It needs no Docker, so the run took about 1.3 seconds.
Exercise 2: Everything About Reference Data Except Smoke
Write a tag expression that selects the reference data tests that aren’t smoke tests, and check how many run.
Hint
Combine reference-data and smoke with & and !. Quote the expression, because ! and & mean something to the shell too.
Solution
1
mvn test -pl module-12 -Dgroups="reference-data & !smoke"
1
2
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
Only pastHolidaysAreKept matches: ParallelExecutionTest has no reference-data tag, though it tests the same API. Tags describe what you wrote, not what a class does.
Summary
You built @SmokeTest and TaggedSuiteTest, and ran the module’s 16 tests in five subsets: everything, smoke, security, everything without Docker, and a tag expression. One suite now serves every stage of a pipeline, from a smoke check in a second and a half to a full run against real infrastructure.
- A tag describes what a test is for; a class’s tags apply to all its methods.
- A composed annotation such as
@SmokeTestmakes an important tag impossible to misspell. - Surefire’s
groupsandexcludedGroupsselect by tag, and Maven profiles give the everyday subsets names. - Tag expressions, quoted, cover the rest:
|,&,!and parentheses. - A wrong tag doesn’t fail the build; it removes tests. Add
-DfailIfNoTests=trueto tag runs.
Here are the commands you used in this guide:
1
2
3
4
5
6
mvn test -pl module-12
mvn test -pl module-12 -Psmoke
mvn test -pl module-12 -Psecurity
mvn test -pl module-12 -Pno-docker
mvn test -pl module-12 -Dgroups="smoke | security"
mvn test -pl module-12 -Dtest=TaggedSuiteTest
What’s Next
Next: you’ve finished Module 12, and the guide. Go back to the REST Assured Learning Guide to revisit any module, or pick a section to apply to your own suite.
The code:
- The finished test class:
TaggedSuiteTest.java - The composed annotation:
SmokeTest.java - The profiles:
module-12/pom.xml - The code and run commands for this module: Module 12 README
Go deeper:
- JUnit User Guide: Tagging and Filtering, for tag syntax and tag expressions.
- Maven Surefire: Using JUnit Platform, for
groupsandexcludedGroups. - Maven: Introduction to Build Profiles, for profiles and how to activate them.
Related guides:
- Inject REST Assured Specifications With a JUnit Extension, for another composed annotation that combines an extension and a tag.
- Read REST Assured Failure Messages, for the deliberately failing tests the parent POM excludes.
