The @ExtendWith annotation tells JUnit to use extra code called extensions when running tests. This helps add features like setup, cleanup, or special checks.
@ExtendWith annotation in JUnit
@ExtendWith(ExtensionClass.class)Place @ExtendWith above your test class or test method.
You can use multiple extensions by listing them in an array: @ExtendWith({Ext1.class, Ext2.class}).
MyExtension to all tests in MyTest class.@ExtendWith(MyExtension.class) public class MyTest { // test methods }
@ExtendWith({ExtensionOne.class, ExtensionTwo.class})
public class MultiExtensionTest {
// test methods
}@ExtendWith(MyExtension.class)
@Test
public void testSomething() {
// test code
}This test class uses @ExtendWith to add SimpleExtension. The extension prints a message before each test runs. The test methods print their own messages.
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; class SimpleExtension implements BeforeEachCallback { @Override public void beforeEach(ExtensionContext context) throws Exception { System.out.println("Before each test: Setup done."); } } @ExtendWith(SimpleExtension.class) public class ExtensionTest { @Test void testOne() { System.out.println("Running testOne"); } @Test void testTwo() { System.out.println("Running testTwo"); } }
Extensions can modify test behavior without changing test code.
Use @ExtendWith to keep tests clean and reusable.
JUnit provides many built-in extensions you can use or customize.
@ExtendWith adds extra behavior to tests using extensions.
Use it on classes or methods to run code before, after, or around tests.
It helps keep tests simple and powerful by reusing common logic.
Practice
@ExtendWith annotation in JUnit 5?Solution
Step 1: Understand the role of
This annotation is used to register extensions that can add behavior before, after, or around tests.@ExtendWithStep 2: Compare with other annotations
Other annotations like@Testmark test methods, but@ExtendWithis specifically for extensions.Final Answer:
To register extensions that add extra behavior to tests -> Option CQuick Check:
@ExtendWith = register extensions [OK]
- Confusing @ExtendWith with @Test
- Thinking it disables tests
- Assuming it controls test order
@ExtendWith to register a custom extension named MyExtension.class on a test class?Solution
Step 1: Recall correct annotation syntax
Annotations use parentheses with class references, like@ExtendWith(MyExtension.class).Step 2: Identify invalid syntax
Curly braces, square brackets, or angle brackets are not valid for this annotation.Final Answer:
@ExtendWith(MyExtension.class) public class TestClass {} -> Option AQuick Check:
Annotation syntax uses parentheses () [OK]
- Using curly braces {} instead of parentheses
- Using square brackets [] or angle brackets <>
- Omitting .class after the class name
@ExtendWith(MyExtension.class)
public class SampleTest {
@Test
void testMethod() {
System.out.println("Test running");
}
}
class MyExtension implements BeforeTestExecutionCallback {
public void beforeTestExecution(ExtensionContext context) {
System.out.println("Before test");
}
}Solution
Step 1: Understand extension behavior
TheMyExtensionimplementsBeforeTestExecutionCallback, so it runs code before the test method.Step 2: Trace output order
First, "Before test" prints from the extension, then "Test running" prints from the test method.Final Answer:
Before test\nTest running -> Option DQuick Check:
Extension beforeTestExecution runs before test method [OK]
- Assuming extension runs after test
- Ignoring extension output
- Confusing output order
@ExtendWith:@ExtendWith
public class FaultyTest {
@Test
void testSomething() {}
}Solution
Step 1: Check
The annotation requires specifying which extension class to use, e.g.,@ExtendWithusage@ExtendWith(MyExtension.class).Step 2: Validate other options
It is valid on classes, test methods do not need to be static or return values.Final Answer:
@ExtendWith is missing the extension class argument -> Option BQuick Check:
@ExtendWith needs extension class specified [OK]
- Omitting the extension class argument
- Thinking @ExtendWith can't be on classes
- Believing test methods must be static
@ExtendWith to achieve this cleanly?Solution
Step 1: Identify reusable setup approach
Implementing a custom extension withBeforeEachCallbackallows running code before each test method.Step 2: Apply
Annotating test classes with@ExtendWithon test classes@ExtendWith(YourExtension.class)reuses the setup logic cleanly across classes.Step 3: Evaluate other options
Manually calling methods or using@BeforeAllis less reusable or runs only once per class. Annotating methods individually is repetitive.Final Answer:
Create a custom extension class implementing BeforeEachCallback and annotate each test class with @ExtendWith(YourExtension.class) -> Option AQuick Check:
Custom extension + @ExtendWith on class = reusable setup [OK]
- Calling setup manually in each test
- Using @BeforeAll for per-test setup
- Annotating each test method instead of class
Ready to prove it?
Pass the test and this pattern moves unknown
