Bird
Raised Fist0
JUnittesting~15 mins

Selecting tests by tags and classes in JUnit - Build an Automation Script

Choose your learning style10 modes available
Run JUnit tests filtered by tags and classes
Preconditions (2)
Step 1: Identify test classes with @Tag("fast") and @Tag("database")
Step 2: Configure the test runner to include only tests tagged with "fast"
Step 3: Run the tests
Step 4: Verify only tests with the "fast" tag are executed
Step 5: Configure the test runner to include tests from a specific test class named "UserServiceTest"
Step 6: Run the tests
Step 7: Verify only tests from the UserServiceTest class are executed
✅ Expected Result: When filtering by tag "fast", only tests with that tag run and pass. When filtering by class UserServiceTest, only tests from that class run and pass.
Automation Requirements - JUnit 5
Assertions Needed:
Verify that only tests with the specified tag are executed
Verify that only tests from the specified class are executed
Best Practices:
Use @Tag annotation to mark tests
Use JUnit Platform Launcher API or build tool configuration to filter tests by tags and classes
Avoid hardcoding test names; use tags and class names for maintainability
Use assertions to confirm test execution results
Automated Solution
JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.engine.DiscoverySelector;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.discovery.TagFilter;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.LauncherFactory;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestFilterByTagAndClass {

    @Tag("fast")
    public static class FastTests {
        @Test
        void fastTest1() {
            assertTrue(true);
        }
    }

    @Tag("database")
    public static class DatabaseTests {
        @Test
        void databaseTest1() {
            assertTrue(true);
        }
    }

    public static class UserServiceTest {
        @Test
        void userServiceTest1() {
            assertTrue(true);
        }
    }

    public static void main(String[] args) {
        runTestsByTag("fast");
        runTestsByClass(UserServiceTest.class);
    }

    private static void runTestsByTag(String tag) {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .filters(TagFilter.includeTags(tag))
                .build();

        Launcher launcher = LauncherFactory.create();
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

        long testsFound = listener.getSummary().getTestsFoundCount();
        long testsSucceeded = listener.getSummary().getTestsSucceededCount();

        System.out.println("Tests run with tag '" + tag + "': " + testsFound);
        assertTrue(testsFound > 0, "No tests found with tag '" + tag + "'");
        assertTrue(testsFound == testsSucceeded, "Some tests with tag '" + tag + "' failed");
    }

    private static void runTestsByClass(Class<?> testClass) {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                .selectors(DiscoverySelectors.selectClass(testClass))
                .build();

        Launcher launcher = LauncherFactory.create();
        SummaryGeneratingListener listener = new SummaryGeneratingListener();

        launcher.registerTestExecutionListeners(listener);
        launcher.execute(request);

        long testsFound = listener.getSummary().getTestsFoundCount();
        long testsSucceeded = listener.getSummary().getTestsSucceededCount();

        System.out.println("Tests run from class '" + testClass.getSimpleName() + "': " + testsFound);
        assertTrue(testsFound > 0, "No tests found in class '" + testClass.getSimpleName() + "'");
        assertTrue(testsFound == testsSucceeded, "Some tests in class '" + testClass.getSimpleName() + "' failed");
    }
}

This code defines three test classes with different tags and one without tags.

The runTestsByTag method builds a test request filtered by the given tag using TagFilter.includeTags. It runs the tests and asserts that some tests were found and all passed.

The runTestsByClass method builds a test request selecting only the specified test class. It runs the tests and asserts that some tests were found and all passed.

Using the JUnit Platform Launcher API allows us to programmatically select tests by tag or class, which is useful for automation and custom test runners.

Common Mistakes - 3 Pitfalls
Using hardcoded test method names instead of tags or classes
Not registering a TestExecutionListener to collect test results
Using deprecated JUnit 4 methods or annotations
Bonus Challenge

Now add data-driven testing with 3 different tag filters: 'fast', 'database', and 'slow'. Run tests for each tag and verify results.

Show Hint

Practice

(1/5)
1. What is the main purpose of using @Tag in JUnit tests?
easy
A. To change the test method names automatically
B. To group tests so you can run specific sets easily
C. To skip tests permanently
D. To define the order in which tests run

Solution

  1. Step 1: Understand the role of @Tag annotation

    The @Tag annotation is used to label tests with a category or group name.
  2. Step 2: Identify the benefit of tagging tests

    Tagging allows you to select and run only tests with specific tags, making test runs focused and faster.
  3. Final Answer:

    To group tests so you can run specific sets easily -> Option B
  4. Quick Check:

    Tags group tests for selective runs [OK]
Hint: Tags group tests for selective execution [OK]
Common Mistakes:
  • Thinking @Tag changes test order
  • Believing @Tag skips tests permanently
  • Assuming @Tag renames test methods
2. Which of the following is the correct way to tag a test method in JUnit 5?
easy
A. @Tag("fast") public void testMethod() {}
B. @Tag fast public void testMethod() {}
C. @Tag:fast public void testMethod() {}
D. @Tag = "fast" public void testMethod() {}

Solution

  1. Step 1: Recall correct annotation syntax in Java

    Annotations use the format @AnnotationName("value") with parentheses and quotes for string values.
  2. Step 2: Match the correct syntax for @Tag

    @Tag("fast") public void testMethod() {} uses @Tag("fast") correctly before the method declaration.
  3. Final Answer:

    @Tag("fast") public void testMethod() {} -> Option A
  4. Quick Check:

    Annotation syntax = @Tag("value") [OK]
Hint: Annotations need parentheses and quotes for string values [OK]
Common Mistakes:
  • Omitting parentheses around tag value
  • Missing quotes around the tag string
  • Using colon or equals instead of parentheses
3. Given the following test class, which tests will run if you execute with the tag filter --include-tag fast?
@Tag("slow")
class ExampleTests {
  @Test
  void testA() {}

  @Test
  @Tag("fast")
  void testB() {}

  @Test
  @Tag("fast")
  void testC() {}
}
medium
A. Only testA
B. All tests: testA, testB, testC
C. Only testB and testC
D. No tests run

Solution

  1. Step 1: Understand tag filtering behavior

    When running with --include-tag fast, only tests tagged with "fast" run.
  2. Step 2: Analyze tags on each test

    Class has @Tag("slow"), so by default tests inherit "slow" tag. testA has no method tag, so only "slow" applies. testB and testC have @Tag("fast") overriding or adding to tags.
  3. Step 3: Determine which tests match "fast" tag

    Only testB and testC have the "fast" tag, so only these run.
  4. Final Answer:

    Only testB and testC -> Option C
  5. Quick Check:

    Include tag "fast" runs only tests tagged "fast" [OK]
Hint: Method tags override class tags for filtering [OK]
Common Mistakes:
  • Assuming class tag applies to all tests ignoring method tags
  • Thinking all tests run without tag filtering
  • Confusing include and exclude tag filters
4. You want to run tests only from the class LoginTests but your command mvn test -Dgroups=LoginTests runs no tests. What is the likely problem?
medium
A. The command should be mvn test -Dtest=LoginTests to run by class
B. JUnit 5 uses tags, not groups; the correct filter is by tags or class name
C. You must add @Group annotation to the class for this to work
D. The class name is case-sensitive and must be lowercase

Solution

  1. Step 1: Understand Maven test filtering options

    Maven Surefire plugin uses -Dtest=ClassName to run tests by class name, not -Dgroups.
  2. Step 2: Identify correct command to run tests by class

    To run tests from LoginTests, use mvn test -Dtest=LoginTests.
  3. Final Answer:

    The command should be mvn test -Dtest=LoginTests to run by class -> Option A
  4. Quick Check:

    Run by class uses -Dtest=ClassName [OK]
Hint: Use -Dtest=ClassName to run tests by class [OK]
Common Mistakes:
  • Using -Dgroups which is for TestNG, not JUnit
  • Assuming class names are lowercase
  • Expecting @Group annotation in JUnit
5. You have two test classes: FastTests tagged with @Tag("fast") and SlowTests tagged with @Tag("slow"). You want to run all tests except those tagged "slow" and only from classes whose names end with "Tests". Which command correctly runs this selection?
hard
A. junit-platform-console --exclude-tag slow --exclude-classname .*Tests
B. junit-platform-console --include-tag fast --include-classname .*Tests
C. junit-platform-console --include-tag slow --include-classname FastTests
D. junit-platform-console --exclude-tag slow --include-classname .*Tests

Solution

  1. Step 1: Understand tag and class name filters

    To exclude slow tests, use --exclude-tag slow. To include only classes ending with "Tests", use --include-classname .*Tests regex.
  2. Step 2: Analyze each option

    junit-platform-console --exclude-tag slow --include-classname .*Tests excludes slow tags and includes classes matching .*Tests, which matches both FastTests and SlowTests classes but excludes slow-tagged tests.
  3. Step 3: Confirm correct filtering

    junit-platform-console --include-tag fast --include-classname .*Tests includes only fast tags, which works but excludes any untagged tests. junit-platform-console --exclude-tag slow --exclude-classname .*Tests excludes classes ending with Tests, which is opposite of requirement. junit-platform-console --include-tag slow --include-classname FastTests includes slow tags, which is opposite of requirement.
  4. Final Answer:

    junit-platform-console --exclude-tag slow --include-classname .*Tests -> Option D
  5. Quick Check:

    Exclude slow tag + include class regex = junit-platform-console --exclude-tag slow --include-classname .*Tests [OK]
Hint: Exclude slow tag and include class regex for correct filtering [OK]
Common Mistakes:
  • Using include-tag slow instead of exclude-tag
  • Excluding classes instead of including
  • Confusing include-tag and exclude-tag usage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard