0
0
JUnittesting~20 mins

@ExtendWith annotation in JUnit - Practice Problems & Coding Challenges

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

What is the primary purpose of the @ExtendWith annotation in JUnit 5?

ATo inject mock objects into test classes automatically
BTo mark a test method as disabled temporarily
CTo specify the order in which test methods should run
DTo register extensions that add additional behavior to test classes or methods
Attempts:
2 left
💡 Hint

Think about how JUnit 5 allows adding extra features to tests beyond basic assertions.

Predict Output
intermediate
2:00remaining
Output of Test with Custom Extension

Given the following JUnit 5 test code using @ExtendWith, what will be printed when the test runs?

JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.*;

class MyExtension implements BeforeTestExecutionCallback {
    @Override
    public void beforeTestExecution(ExtensionContext context) {
        System.out.println("Before test: " + context.getDisplayName());
    }
}

@ExtendWith(MyExtension.class)
class SampleTest {
    @Test
    void testMethod() {
        System.out.println("Inside test method");
    }
}
A
Before test: testMethod(SampleTest)
Inside test method
B
Inside test method
Before test: testMethod(SampleTest)
CNo output printed
DOnly Inside test method
Attempts:
2 left
💡 Hint

Consider when the beforeTestExecution callback runs relative to the test method.

assertion
advanced
1:30remaining
Correct Assertion for Extension Registration

Which assertion correctly verifies that a test class is registered with a specific extension using @ExtendWith in JUnit 5?

AassertTrue(testClass.isAnnotationPresent(ExtendWith.class))
BassertEquals(ExtendWith.class, testClass.getAnnotation(ExtendWith.class))
CassertNotNull(testClass.getAnnotation(ExtendWith.class))
DassertFalse(testClass.isAnnotationPresent(ExtendWith.class))
Attempts:
2 left
💡 Hint

Check how to verify if an annotation is present on a class.

🔧 Debug
advanced
2:00remaining
Debugging Extension Not Invoked

A developer uses @ExtendWith(MyExtension.class) on a test class, but the extension's methods are never called during test execution. What is the most likely cause?

AThe extension class is not public
BThe test class is missing the <code>@Test</code> annotation on methods
CThe extension class does not implement any JUnit 5 extension interfaces
DJUnit 5 does not support <code>@ExtendWith</code> on classes
Attempts:
2 left
💡 Hint

Think about what JUnit 5 requires for an extension to be recognized and invoked.

framework
expert
2:30remaining
Combining Multiple Extensions with @ExtendWith

Consider a test class annotated with @ExtendWith({ExtA.class, ExtB.class}). Which statement about the order of extension callbacks is true?

AExtensions are invoked in reverse order, so ExtB callbacks run before ExtA callbacks
BExtensions are invoked in the order they appear in the annotation, so ExtA callbacks run before ExtB callbacks
CExtension invocation order is random and cannot be controlled
DJUnit 5 merges extensions and runs their callbacks simultaneously
Attempts:
2 left
💡 Hint

Think about how JUnit 5 processes multiple extensions declared together.