Extensions let you add or change how JUnit runs tests. This helps make tests easier and more powerful without changing JUnit itself.
Why extensions customize JUnit behavior
Introduction
Syntax
JUnit
@ExtendWith(MyExtension.class) public class MyTest { // test methods }
Use @ExtendWith to tell JUnit to use your extension.
Extensions are classes that implement JUnit extension interfaces.
Examples
JUnit
@ExtendWith(TimingExtension.class) public class MyTest { @Test void testSomething() { // test code } }
JUnit
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; public class RetryExtension implements TestExecutionExceptionHandler { @Override public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { // retry logic here } } @ExtendWith(RetryExtension.class) public class MyTest { @Test void testWithRetry() { // test code } }
Sample Program
This test class uses a simple extension that prints a message before each test runs.
JUnit
import org.junit.jupiter.api.extension.*; import org.junit.jupiter.api.*; class SimpleExtension implements BeforeEachCallback { @Override public void beforeEach(ExtensionContext context) { System.out.println("Before each test: " + context.getDisplayName()); } } @ExtendWith(SimpleExtension.class) public class MyTest { @Test void testOne() { System.out.println("Running testOne"); Assertions.assertTrue(true); } @Test void testTwo() { System.out.println("Running testTwo"); Assertions.assertEquals(2, 1 + 1); } }
Important Notes
Extensions help keep your test code clean by moving repeated setup or checks outside test methods.
You can combine multiple extensions on one test class.
JUnit provides many built-in extension points to customize behavior.
Summary
Extensions let you add extra behavior to JUnit tests without changing test code.
Use @ExtendWith to apply extensions to test classes or methods.
Extensions can run code before/after tests, handle exceptions, or change test data.
Practice
1. What is the main reason to use extensions in JUnit tests?
easy
Solution
Step 1: Understand the purpose of extensions
Extensions allow adding extra behavior like setup or cleanup without modifying test methods.Step 2: Compare options with this purpose
Only To add extra behavior without changing the test code matches this purpose; others describe unrelated goals.Final Answer:
To add extra behavior without changing the test code -> Option AQuick Check:
Extensions add behavior = D [OK]
Hint: Extensions add behavior without changing tests [OK]
Common Mistakes:
- Thinking extensions speed up writing tests
- Confusing extensions with replacing JUnit
- Assuming extensions create UI
2. Which annotation is used to apply an extension to a JUnit test class or method?
easy
Solution
Step 1: Recall the correct annotation for extensions
The standard JUnit annotation to apply extensions is@ExtendWith.Step 2: Verify other options
Other options are not valid JUnit annotations for extensions.Final Answer:
@ExtendWith -> Option CQuick Check:
@ExtendWith = C [OK]
Hint: Remember '@ExtendWith' applies extensions [OK]
Common Mistakes:
- Using @TestExtension which does not exist
- Confusing with @UseExtension or @EnableExtension
- Forgetting the '@' symbol
3. Given this code snippet, what will be printed when the test runs?
Assuming
@ExtendWith(MyExtension.class)
class MyTest {
@Test
void testMethod() {
System.out.println("Test running");
}
}Assuming
MyExtension prints "Before test" before the test and "After test" after the test.medium
Solution
Step 1: Understand extension behavior
MyExtension runs code before and after the test method, printing "Before test" and "After test" respectively.Step 2: Order of output
Output order is: before test message, test method message, after test message.Final Answer:
Before test\nTest running\nAfter test -> Option BQuick Check:
Extension runs before/after test = A [OK]
Hint: Extensions run code before and after tests [OK]
Common Mistakes:
- Assuming extension runs after test only
- Mixing order of printed lines
- Ignoring extension output
4. Identify the error in this JUnit test using an extension:
Why might
@ExtendWith(MyExtension.class)
class TestClass {
@Test
void test() {
System.out.println("Running test");
}
void helper() {
System.out.println("Helper method");
}
}Why might
helper() not be called during test execution?medium
Solution
Step 1: Check method annotations
Only methods annotated with@Testrun as tests; helper() lacks this annotation.Step 2: Understand extension role
Extensions do not disable helper methods; they only affect test lifecycle.Final Answer:
Helper method is not annotated with @Test -> Option DQuick Check:
Missing @Test means no run = B [OK]
Hint: Only @Test methods run automatically [OK]
Common Mistakes:
- Thinking extensions disable helper methods
- Believing helper must be static or private
- Assuming all methods run as tests
5. You want to create a JUnit extension that changes test data before each test runs. Which approach best uses extensions to achieve this?
hard
Solution
Step 1: Identify extension interface for pre-test actions
TheBeforeEachCallbackinterface allows running code before each test method.Step 2: Apply this to modify test data
Modifying test data inbeforeEach()method is the correct way to customize data before tests.Final Answer:
Implement BeforeEachCallback and modify test data in beforeEach() -> Option AQuick Check:
BeforeEachCallback modifies data before tests = A [OK]
Hint: Use BeforeEachCallback to run code before tests [OK]
Common Mistakes:
- Trying to override test methods directly
- Misusing @Test parameters for data changes
- Creating many classes instead of using extensions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
