0
0
JUnittesting~20 mins

Selecting tests by tags and classes in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Tag & Class Test Selector Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of JUnit test run with tag filter
Given the following JUnit 5 test classes with tags, what will be the output when running tests with the tag @Tag("fast") only?
JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

    @Test
    @Tag("slow")
    void slowTest1() {
        assertTrue(true);
    }
}

public class MixedTests {
    @Test
    @Tag("fast")
    void fastTest2() {
        assertTrue(true);
    }

    @Test
    void noTagTest() {
        assertTrue(true);
    }
}
AOnly slowTest1 runs because it has a tag; others are skipped.
BAll tests run regardless of tags and all pass.
COnly tests without tags run; fastTest1, fastTest2, and slowTest1 are skipped.
DOnly fastTest1 and fastTest2 run and pass; slowTest1 and noTagTest are skipped.
Attempts:
2 left
💡 Hint
Remember that JUnit 5 allows filtering tests by tags using the @Tag annotation and test runners can include or exclude tests by tag.
assertion
intermediate
1:30remaining
Correct assertion to verify only tagged tests ran
You run JUnit tests filtered by tag "integration". Which assertion correctly verifies that only tests with this tag ran?
JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

// Assume testResults is a list of test names that ran
List<String> testResults = List.of("integrationTest1", "integrationTest2");
AassertEquals(2, testResults.size());
BassertEquals(4, testResults.size());
CassertEquals(0, testResults.size());
DassertEquals(1, testResults.size());
Attempts:
2 left
💡 Hint
Count how many tests have the "integration" tag and expect only those to run.
🔧 Debug
advanced
2:00remaining
Why does filtering by tag fail to exclude untagged tests?
You configured your JUnit 5 test run to include only tests with tag "ui" but untagged tests still run. What is the most likely cause?
AThe test runner configuration is missing the tag filter include directive.
BTests without tags are always included by default and cannot be excluded.
CJUnit 5 does not support filtering by tags.
DThe @Tag annotation is only for documentation and does not affect test execution.
Attempts:
2 left
💡 Hint
Check your test runner or build tool configuration for tag filtering settings.
framework
advanced
1:30remaining
Selecting tests by class and tag in JUnit 5
Which JUnit 5 command line option runs only tests in class LoginTests tagged with smoke?
Ajunit-platform-console --select-package=LoginTests --include-tags=smoke
Bjunit-platform-console --select-package=LoginTests --exclude-tags=smoke
Cjunit-platform-console --select-class=LoginTests --include-tags=smoke
Djunit-platform-console --select-class=LoginTests --exclude-tags=smoke
Attempts:
2 left
💡 Hint
Use --select-class to specify the test class and --include-tags to filter by tag.
🧠 Conceptual
expert
2:30remaining
Impact of multiple tags on test selection
If a test method is annotated with @Tag("fast") and @Tag("database"), and the test run includes only tests tagged "fast", what happens?
AThe test is excluded because it has more than one tag.
BThe test runs because it has the "fast" tag, even though it also has "database".
CThe test runs only if both tags are included in the filter.
DThe test is excluded because it has the "database" tag.
Attempts:
2 left
💡 Hint
Think about how tag filters include tests that have at least one matching tag.