What if you could run only the tests you need and skip the rest with a simple label?
Why @Tag for categorization in JUnit? - Purpose & Use Cases
Imagine you have a big set of tests for your app, but you want to run only the ones related to login or payment. Without any way to group them, you have to run all tests every time.
Running all tests manually takes a lot of time and effort. You might miss important tests or run irrelevant ones. It's easy to get confused and waste hours waiting for results.
The @Tag annotation lets you label tests with categories like 'login' or 'payment'. Then you can quickly run just the tests you want, saving time and focusing on what matters.
void testLogin() { ... } // no tag, run all tests
void testPayment() { ... } // no tag, run all tests@Tag("login") void testLogin() { ... } @Tag("payment") void testPayment() { ... }
You can easily organize and run specific groups of tests, making testing faster and smarter.
When fixing a bug in the login feature, you run only tests tagged 'login' to quickly check your changes without waiting for unrelated tests.
Manual test runs waste time and cause confusion.
@Tag groups tests by category for easy selection.
This speeds up testing and improves focus on relevant areas.
Practice
@Tag annotation in JUnit?Solution
Step 1: Understand the role of
@Tag@Tagis used to label tests with categories or groups.Step 2: Identify the purpose of grouping
Grouping helps to select or exclude tests easily during test runs.Final Answer:
To group tests for easier selection and filtering -> Option CQuick Check:
@Taggroups tests = A [OK]
- Confusing @Tag with test execution order
- Thinking @Tag sets test timeouts
- Assuming @Tag marks tests deprecated
Solution
Step 1: Recall correct annotation syntax
Annotations use parentheses with string literals for parameters, e.g., @Tag("fast").Step 2: Check each option's syntax
Only @Tag("fast") public void testMethod() {} uses correct parentheses and quotes syntax.Final Answer:
@Tag("fast") public void testMethod() {} -> Option BQuick Check:
Annotation syntax = parentheses + quotes [OK]
- Using square brackets instead of parentheses
- Omitting quotes around tag name
- Using curly braces or colons incorrectly
@Tag("integration")
class DatabaseTests {
@Test void testConnection() {}
@Test @Tag("slow") void testQueryPerformance() {}
}
@Tag("unit")
class ServiceTests {
@Test void testServiceLogic() {}
}Solution
Step 1: Understand class-level and method-level tags
Class DatabaseTests is tagged "integration", so all its tests inherit this tag unless excluded.Step 2: Check method tags inside DatabaseTests
testConnection() inherits "integration" tag; testQueryPerformance() has both "integration" (from class) and "slow" tags.Step 3: Check ServiceTests tags
ServiceTests is tagged "unit", so its tests do not run when filtering by "integration".Final Answer:
Both testConnection() and testQueryPerformance() -> Option DQuick Check:
Class tag applies to all methods unless excluded [OK]
- Ignoring class-level tags on methods
- Assuming method tags override class tags completely
- Running tests with unrelated tags
@Test
@Tag(fast)
void quickTest() {}Solution
Step 1: Check syntax of @Tag annotation
Tag names must be string literals in quotes, e.g., @Tag("fast").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.Final Answer:
Missing quotes around the tag name "fast" -> Option AQuick Check:
Tag names need quotes [OK]
- Forgetting quotes around tag strings
- Thinking annotation order matters here
- Assuming method name affects tag filtering
Solution
Step 1: Understand tag filtering options
JUnit 5 supports including and excluding tags separately with --include-tag and --exclude-tag options.Step 2: Analyze each option
--include-tag fast --exclude-tag database includes tests tagged "fast" but excludes those tagged "database", matching the requirement.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.Final Answer:
--include-tag fast --exclude-tag database -> Option AQuick Check:
Include and exclude tags use separate options [OK]
- Combining tags with commas in --tags option
- Using wrong flag names like --exclude-tags
- Reversing include and exclude logic
Ready to prove it?
Pass the test and this pattern moves unknown
