The JUnit Extension Model allows you to add custom behavior to tests, such as setup, teardown, or modifying test execution, without changing the test code itself.
Click to reveal answer
beginner
Name two common extension points in JUnit 5's Extension Model.
Two common extension points are BeforeEachCallback and AfterEachCallback, which run code before and after each test method respectively.
Click to reveal answer
beginner
How do you register an extension in JUnit 5?
You can register an extension by annotating the test class or method with <code>@ExtendWith(YourExtension.class)</code>.
Click to reveal answer
intermediate
What is the difference between BeforeAllCallback and BeforeEachCallback?
BeforeAllCallback runs once before all tests in a class, while BeforeEachCallback runs before each individual test method.
Click to reveal answer
intermediate
Why is the JUnit Extension Model useful for test code maintenance?
It helps keep test code clean by separating reusable setup or teardown logic into extensions, making tests easier to read and maintain.
Click to reveal answer
Which annotation is used to apply a JUnit 5 extension to a test class?
A@ExtendWith
B@TestExtension
C@UseExtension
D@EnableExtension
✗ Incorrect
The correct annotation to register an extension in JUnit 5 is @ExtendWith.
What does the AfterEachCallback interface allow you to do?
ARun code before all tests
BModify test method parameters
CRun code after each test method
DSkip tests conditionally
✗ Incorrect
AfterEachCallback runs code after each test method finishes.
Which of these is NOT a valid JUnit 5 extension point?
ABeforeAllCallback
BAfterEachCallback
CBeforeTestExecutionCallback
DTestCaseListener
✗ Incorrect
TestCaseListener is not a JUnit 5 extension point.
How often does BeforeAllCallback run in a test class?
ABefore each test method
BOnce before all test methods
CAfter each test method
DAfter all test methods
✗ Incorrect
BeforeAllCallback runs once before all tests in the class.
What is a key benefit of using the JUnit Extension Model?
AAdding reusable behavior without cluttering test code
BWriting tests without assertions
CAutomatically generating test data
DRunning tests in parallel
✗ Incorrect
Extensions let you add reusable setup or teardown logic cleanly.
Explain how the JUnit Extension Model helps improve test code organization.
Think about how extensions reduce repeated code in tests.
You got /4 concepts.
Describe the difference between BeforeEachCallback and BeforeAllCallback in JUnit extensions.
Consider how often each callback runs during testing.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the JUnit extension model?
easy
A. To add extra behavior to tests without changing the test code
B. To write test cases faster using templates
C. To automatically generate test data
D. To replace assertions with custom logic
Solution
Step 1: Understand the extension model concept
The extension model allows adding extra behavior to tests without modifying the original test code.
Step 2: Compare options with the concept
Only To add extra behavior to tests without changing the test code correctly describes this purpose. Other options describe unrelated features.
Final Answer:
To add extra behavior to tests without changing the test code -> Option A
Quick Check:
Extension model purpose = add behavior without code change [OK]
Hint: Extension model = add behavior without changing tests [OK]
Common Mistakes:
Confusing extension model with test data generation
Thinking it replaces assertions
Assuming it speeds up test writing
2. Which annotation is used to apply an extension to a JUnit test class?
easy
A. @ExtendWith
B. @TestExtension
C. @UseExtension
D. @EnableExtension
Solution
Step 1: Recall the correct annotation for extensions
JUnit uses @ExtendWith to apply extensions to test classes or methods.
Step 2: Verify other options
Options B, C, and D are not valid JUnit annotations for extensions.
Final Answer:
@ExtendWith -> Option A
Quick Check:
Extension annotation = @ExtendWith [OK]
Hint: Remember extension annotation is @ExtendWith [OK]
Common Mistakes:
Using @TestExtension which does not exist
Confusing with @UseExtension or @EnableExtension
Forgetting the '@' symbol
3. Given the following code snippet, what will be printed when the test runs?
@ExtendWith(MyExtension.class)
class MyTest {
@Test
void testMethod() {
System.out.println("Test running");
}
}
class MyExtension implements BeforeTestExecutionCallback {
public void beforeTestExecution(ExtensionContext context) {
System.out.println("Before test");
}
}
medium
A. Test running
Before test
B. Before test
Test running
C. Test running
D. Before test
Solution
Step 1: Understand the extension callback
The BeforeTestExecutionCallback runs its method before the test method executes, printing "Before test" first.
Step 2: Order of print statements
First "Before test" prints, then the test method prints "Test running".
Final Answer:
Before test\nTest running -> Option B
Quick Check:
BeforeTestExecutionCallback runs before test method [OK]
Hint: BeforeTestExecutionCallback runs before test method [OK]
Common Mistakes:
Assuming test method prints first
Confusing callback timing
Ignoring extension execution order
4. Identify the error in this extension implementation:
public class MyExtension implements BeforeTestExecutionCallback {
public void beforeTestExecution() {
System.out.println("Before test");
}
}
medium
A. Method beforeTestExecution should return boolean
B. Method name should be beforeTestStart
C. Class must extend AbstractExtension instead of implementing interface
D. Missing parameter ExtensionContext in beforeTestExecution method
Solution
Step 1: Check method signature for interface implementation
The interface BeforeTestExecutionCallback requires beforeTestExecution(ExtensionContext context) method with one parameter.
Step 2: Identify missing parameter
The method in the code has no parameters, so it does not correctly implement the interface.
Final Answer:
Missing parameter ExtensionContext in beforeTestExecution method -> Option D
Hint: Check method parameters match interface exactly [OK]
Common Mistakes:
Omitting required method parameters
Changing method return type incorrectly
Using wrong method names
5. You want to create a JUnit extension that logs a message before and after each test method runs. Which combination of interfaces should your extension implement?
hard
A. BeforeTestExecutionCallback and AfterTestExecutionCallback
B. BeforeAllCallback and AfterAllCallback
C. BeforeEachCallback and AfterEachCallback
D. TestExecutionExceptionHandler and BeforeEachCallback
Solution
Step 1: Understand callback interfaces for test methods
BeforeEachCallback runs before each test method, and AfterEachCallback runs after each test method.
Step 2: Match requirement to interfaces
To log before and after each test method, implement both BeforeEachCallback and AfterEachCallback.
Final Answer:
BeforeEachCallback and AfterEachCallback -> Option C
Quick Check:
Before/After each test = BeforeEachCallback + AfterEachCallback [OK]
Hint: Before/After each test = BeforeEachCallback + AfterEachCallback [OK]
Common Mistakes:
Using BeforeAllCallback which runs once per class
Confusing AfterTestExecutionCallback with AfterEachCallback
Choosing exception handler interfaces incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.