Bird
Raised Fist0
JUnittesting~20 mins

@Tag for categorization in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
JUnit @Tag Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Purpose of @Tag in JUnit 5

What is the main purpose of using the @Tag annotation in JUnit 5 tests?

ATo mark tests that should be ignored during execution.
BTo define the order in which tests are executed.
CTo categorize tests so you can run specific groups of tests selectively.
DTo specify the timeout duration for a test method.
Attempts:
2 left
💡 Hint

Think about how you might run only some tests and not all.

❓ Predict Output
intermediate
2:00remaining
Output of Tagged Test Execution

Given the following JUnit 5 test class, what tests will run if you execute tests with the tag integration?

JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class SampleTest {

    @Test
    @Tag("unit")
    void unitTest() {
        System.out.println("Unit test running");
    }

    @Test
    @Tag("integration")
    void integrationTest() {
        System.out.println("Integration test running");
    }

    @Test
    void noTagTest() {
        System.out.println("No tag test running");
    }
}
AOnly 'Integration test running' is printed.
BAll three tests print their messages.
C'Unit test running' and 'Integration test running' are printed.
DOnly 'No tag test running' is printed.
Attempts:
2 left
💡 Hint

Remember that running tests by tag only runs tests with that tag.

❓ assertion
advanced
2:30remaining
Correct Assertion for Tag Presence

Which assertion correctly verifies that a test method is annotated with the tag slow using JUnit 5's reflection API?

JUnit
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Method;
import static org.junit.jupiter.api.Assertions.*;

public class TagTest {

    @Test
    @Tag("slow")
    void slowTest() {}

    @Test
    void checkTag() throws Exception {
        Method method = TagTest.class.getMethod("slowTest");
        // Which assertion below is correct?
    }
}
AassertFalse(method.isAnnotationPresent(Tag.class));
BassertTrue(method.isAnnotationPresent(Tag.class) && method.getAnnotation(Tag.class).value().equals("slow"));
CassertTrue(method.getAnnotationsByType(Tag.class).length == 1);
DassertEquals("slow", method.getAnnotation(Tag.class).value());
Attempts:
2 left
💡 Hint

Check both presence and value of the annotation.

🔧 Debug
advanced
2:00remaining
Why Does This Tagged Test Not Run?

Consider this JUnit 5 test class:

import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class DebugTest {

    @Test
    @Tag("fast")
    void fastTest() {
        System.out.println("Fast test");
    }

    @Test
    @Tag("slow")
    void slowTest() {
        System.out.println("Slow test");
    }
}

You run tests with the tag fast but fastTest does not run. What is the most likely reason?

AThe test class must be annotated with @Tag for methods to be filtered.
BThe @Tag annotation is only for documentation and does not affect test execution.
CThe test method name must start with 'test' to run.
DThe test runner was not configured to include the 'fast' tag in the filter.
Attempts:
2 left
💡 Hint

Think about how test runners select tests by tag.

❓ framework
expert
3:00remaining
Combining @Tag with Test Suites in JUnit 5

You want to create a test suite that runs only tests tagged with critical across multiple test classes. Which approach correctly achieves this in JUnit 5?

AUse <code>@SelectPackages</code> or <code>@SelectClasses</code> with <code>@IncludeTags("critical")</code> on a suite class annotated with <code>@Suite</code>.
BAnnotate the suite class with <code>@Tag("critical")</code> and run it normally.
CManually call each test method with the 'critical' tag inside the suite class.
DUse <code>@RunWith(JUnitPlatform.class)</code> and filter tags in the test methods.
Attempts:
2 left
💡 Hint

JUnit 5 suites use specific annotations to select tests and filter tags.

Practice

(1/5)
1. What is the main purpose of the @Tag annotation in JUnit?
easy
A. To mark tests as deprecated
B. To define the order in which tests run
C. To group tests for easier selection and filtering
D. To specify the timeout for a test

Solution

  1. Step 1: Understand the role of @Tag

    @Tag is used to label tests with categories or groups.
  2. Step 2: Identify the purpose of grouping

    Grouping helps to select or exclude tests easily during test runs.
  3. Final Answer:

    To group tests for easier selection and filtering -> Option C
  4. Quick Check:

    @Tag groups tests = A [OK]
Hint: Tags group tests to run or skip categories fast [OK]
Common Mistakes:
  • Confusing @Tag with test execution order
  • Thinking @Tag sets test timeouts
  • Assuming @Tag marks tests deprecated
2. Which of the following is the correct syntax to tag a test method with the tag "fast" 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

    Annotations use parentheses with string literals for parameters, e.g., @Tag("fast").
  2. Step 2: Check each option's syntax

    Only @Tag("fast") public void testMethod() {} uses correct parentheses and quotes syntax.
  3. Final Answer:

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

    Annotation syntax = parentheses + quotes [OK]
Hint: Use parentheses and quotes for annotation parameters [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Omitting quotes around tag name
  • Using curly braces or colons incorrectly
3. Given the following test class, which tests will run if you execute tests with the tag "integration"?
@Tag("integration")
class DatabaseTests {
  @Test void testConnection() {}
  @Test @Tag("slow") void testQueryPerformance() {}
}

@Tag("unit")
class ServiceTests {
  @Test void testServiceLogic() {}
}
medium
A. All three tests
B. Only testQueryPerformance()
C. Only testServiceLogic()
D. Both testConnection() and testQueryPerformance()

Solution

  1. Step 1: Understand class-level and method-level tags

    Class DatabaseTests is tagged "integration", so all its tests inherit this tag unless excluded.
  2. Step 2: Check method tags inside DatabaseTests

    testConnection() inherits "integration" tag; testQueryPerformance() has both "integration" (from class) and "slow" tags.
  3. Step 3: Check ServiceTests tags

    ServiceTests is tagged "unit", so its tests do not run when filtering by "integration".
  4. Final Answer:

    Both testConnection() and testQueryPerformance() -> Option D
  5. Quick Check:

    Class tag applies to all methods unless excluded [OK]
Hint: Class tags apply to all methods inside by default [OK]
Common Mistakes:
  • Ignoring class-level tags on methods
  • Assuming method tags override class tags completely
  • Running tests with unrelated tags
4. You wrote the following test method but it does not run when you filter tests by the tag "fast". What is the likely problem?
@Test
@Tag(fast)
void quickTest() {}
medium
A. Missing quotes around the tag name "fast"
B. The @Test annotation must come after @Tag
C. The method name must start with 'test'
D. The tag name "fast" is not allowed in JUnit

Solution

  1. Step 1: Check syntax of @Tag annotation

    Tag names must be string literals in quotes, e.g., @Tag("fast").
  2. Step 2: Identify the error in the code

    Here, fast is unquoted, so it is treated as an undefined variable, causing the tag to be ignored.
  3. Final Answer:

    Missing quotes around the tag name "fast" -> Option A
  4. Quick Check:

    Tag names need quotes [OK]
Hint: Always put tag names inside quotes [OK]
Common Mistakes:
  • Forgetting quotes around tag strings
  • Thinking annotation order matters here
  • Assuming method name affects tag filtering
5. You want to run only tests tagged as "fast" but exclude those also tagged "database". Which JUnit 5 command line option correctly achieves this?
hard
A. --include-tag fast --exclude-tag database
B. --tags fast,database
C. --include-tag database --exclude-tag fast
D. --tags fast --exclude-tags database

Solution

  1. Step 1: Understand tag filtering options

    JUnit 5 supports including and excluding tags separately with --include-tag and --exclude-tag options.
  2. Step 2: Analyze each option

    --include-tag fast --exclude-tag database includes tests tagged "fast" but excludes those tagged "database", matching the requirement.
  3. Step 3: Identify incorrect syntax in other options

    Options B and D misuse --tags or --exclude-tags flags; --include-tag database --exclude-tag fast reverses include/exclude logic.
  4. Final Answer:

    --include-tag fast --exclude-tag database -> Option A
  5. Quick Check:

    Include and exclude tags use separate options [OK]
Hint: Use separate include and exclude tag options [OK]
Common Mistakes:
  • Combining tags with commas in --tags option
  • Using wrong flag names like --exclude-tags
  • Reversing include and exclude logic
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