0
0
JUnittesting~20 mins

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

Choose your learning style9 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.