True or False: @Tag annotations can be used on test classes as well as test methods.
True. You can put <code>@Tag</code> on a test class to tag all tests inside it, or on individual test methods for more specific categorization.
Click to reveal answer
intermediate
How can you run only tests with a specific tag using JUnit?
You configure your test runner or build tool (like Maven or Gradle) to include only tests with a specific @Tag. For example, in Maven Surefire plugin, you can specify -Dgroups=fast to run tests tagged with @Tag("fast").
Click to reveal answer
beginner
What is a practical example of using @Tag in a real project?
You might tag tests as @Tag("integration") for slow tests that hit a database, and @Tag("unit") for fast, isolated tests. This way, you can run only unit tests during development for speed, and run integration tests separately.
Click to reveal answer
What does the @Tag annotation do in JUnit?
AMarks a test as ignored
BDefines the order of test execution
CCategorizes tests for selective execution
DSpecifies the test timeout
✗ Incorrect
@Tag is used to group tests so you can run or exclude them by category.
Can you apply multiple @Tag annotations to a single test method?
ANo, tags must be applied at class level only
BNo, only one tag is allowed
CYes, but only if tags are comma-separated in one annotation
DYes, by adding multiple <code>@Tag</code> annotations
✗ Incorrect
JUnit allows multiple @Tag annotations on the same method to assign multiple categories.
Where can @Tag annotations be placed?
AOnly on test methods
BOn both test classes and test methods
COnly on the test suite
DOnly on test classes
✗ Incorrect
@Tag can be used on classes to tag all tests inside, or on individual methods.
How do you run tests with a specific tag using Maven?
AUse <code>-Dgroups=tagName</code>
BUse <code>-Dtest=tagName</code>
CUse <code>-Dtags=tagName</code>
DUse <code>-DincludeTags=tagName</code>
✗ Incorrect
Maven Surefire plugin uses -Dgroups=tagName to run tests with that tag.
Why is tagging tests useful in a project?
ATo speed up test runs by selecting relevant tests
BTo automatically fix failing tests
CTo generate test data
DTo encrypt test results
✗ Incorrect
Tagging helps run only needed tests, saving time during development or CI.
Explain how you would use @Tag annotations to organize tests in a JUnit project.
Think about grouping tests for easier execution control.
You got /4 concepts.
Describe the steps to run only tests with a specific tag using a build tool like Maven.
Focus on how tagging connects with test execution commands.
You got /4 concepts.
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
Step 1: Understand the role of @Tag
@Tag is 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 C
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
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 B
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"?