What is the main purpose of using the @Tag annotation in JUnit 5 tests?
Think about how you might run only some tests and not all.
The @Tag annotation allows you to label tests with categories. This helps when you want to run only tests with certain tags, for example, 'fast' or 'database'.
Given the following JUnit 5 test class, what tests will run if you execute tests with the tag integration?
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"); } }
Remember that running tests by tag only runs tests with that tag.
When running tests filtered by the tag 'integration', only tests annotated with @Tag("integration") run. Tests without this tag or with other tags are skipped.
Which assertion correctly verifies that a test method is annotated with the tag slow using JUnit 5's reflection API?
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? } }
Check both presence and value of the annotation.
Option B checks that the @Tag annotation is present and that its value is exactly 'slow'. Option B would fail if the annotation is missing (NullPointerException). Option B only checks presence count, not value. Option B is incorrect.
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?
Think about how test runners select tests by tag.
Tags only affect which tests run if the test runner is told to include or exclude tests by tag. If the runner is not configured to run 'fast' tagged tests, those tests won't run.
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?
JUnit 5 suites use specific annotations to select tests and filter tags.
JUnit 5 provides @Suite with @SelectPackages or @SelectClasses to define which tests to run, combined with @IncludeTags to filter by tag. Option A is the correct modern approach.