A custom extension in JUnit is a user-defined class that adds extra behavior to tests, such as setup, teardown, or conditional test execution, by implementing JUnit's Extension API.
Click to reveal answer
beginner
Which JUnit interface do you implement to create a custom extension that runs code before and after each test?
You implement the BeforeEachCallback and AfterEachCallback interfaces to run code before and after each test method.
Click to reveal answer
beginner
How do you register a custom extension in a JUnit 5 test class?
You register a custom extension by annotating the test class with <code>@ExtendWith(YourExtension.class)</code>.
Click to reveal answer
intermediate
What is the purpose of the ExtensionContext parameter in JUnit custom extensions?
The ExtensionContext provides information about the current test, such as test method, test class, and a store for sharing data between callbacks.
Click to reveal answer
intermediate
Name two common use cases for creating custom JUnit extensions.
Common use cases include managing external resources (like databases or servers) and implementing conditional test execution based on environment or configuration.
Click to reveal answer
Which annotation is used to apply a custom extension to a JUnit 5 test class?
A@UseExtension
B@CustomExtension
C@ExtendWith
D@RegisterExtension
✗ Incorrect
The correct annotation to register a custom extension is @ExtendWith. @RegisterExtension is used for field-level extensions.
Which interface should you implement to run code before all tests in a test class?
ABeforeEachCallback
BTestWatcher
CTestExecutionListener
DBeforeAllCallback
✗ Incorrect
BeforeAllCallback runs code once before all tests. BeforeEachCallback runs before each test.
What does the ExtensionContext NOT provide in a custom extension?
AAbility to modify test source code
BInformation about the current test method
CAccess to test class instance
DA store to share data between callbacks
✗ Incorrect
ExtensionContext provides test info and data sharing but cannot modify test source code.
How can you share data between different callbacks in a custom extension?
AUsing the ExtensionContext's store
BUsing static variables
CUsing global variables
DUsing environment variables
✗ Incorrect
The ExtensionContext provides a Store to safely share data between callbacks.
Which of the following is NOT a typical use case for custom JUnit extensions?
AConditional test execution
BChanging Java language syntax
CManaging external resources
DLogging test lifecycle events
✗ Incorrect
Custom extensions cannot change Java syntax; they add behavior around tests.
Explain how to create and register a custom extension in JUnit 5.
Think about lifecycle interfaces and how JUnit knows to use your extension.
You got /3 concepts.
Describe two practical scenarios where custom JUnit extensions improve testing.
Consider what repetitive tasks or conditions you want to automate in tests.
You got /2 concepts.
Practice
(1/5)
1. What is the main purpose of using a custom extension in JUnit with @ExtendWith?
easy
A. To generate test reports in different formats
B. To write test cases faster by skipping assertions
C. To change the Java version used by the test runner
D. To add reusable code that runs before or after tests automatically
Solution
Step 1: Understand the role of custom extensions
Custom extensions allow adding reusable code that hooks into the test lifecycle, such as setup or teardown actions.
Step 2: Recognize the use of @ExtendWith
The annotation @ExtendWith applies these extensions to tests so the extra code runs automatically before or after tests.
Final Answer:
To add reusable code that runs before or after tests automatically -> Option D
Quick Check:
Custom extensions = reusable lifecycle code [OK]
Hint: Remember @ExtendWith adds reusable code around tests [OK]
Common Mistakes:
Thinking extensions skip assertions
Confusing extensions with Java version settings
Assuming extensions generate reports
2. Which of the following is the correct syntax to apply a custom extension class named MyExtension to a JUnit 5 test class?
easy
A. @ExtendWith(MyExtension.class)
B. @UseExtension(MyExtension)
C. @EnableExtension(MyExtension.class)
D. @ApplyExtension(MyExtension.class)
Solution
Step 1: Recall the correct annotation for extensions
JUnit 5 uses @ExtendWith to apply extensions to test classes or methods.
Step 2: Check the syntax for specifying the extension class
The extension class must be passed as a class literal with .class, so @ExtendWith(MyExtension.class) is correct.
Final Answer:
@ExtendWith(MyExtension.class) -> Option A
Quick Check:
@ExtendWith + .class = correct syntax [OK]
Hint: Use @ExtendWith with .class for extension classes [OK]
Common Mistakes:
Omitting .class after the extension name
Using wrong annotation names like @UseExtension
Passing extension name without parentheses
3. Given the following extension code, what will be printed when the test runs?
public class PrintExtension implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) {
System.out.println("Before test: " + context.getDisplayName());
}
}
@ExtendWith(PrintExtension.class)
public class SampleTest {
@Test
void testOne() {}
}
medium
A. Before test: testOne()
B. Before test: SampleTest
C. No output printed
D. Compilation error due to missing interface
Solution
Step 1: Understand BeforeEachCallback behavior
The beforeEach method runs before each test method and prints the test's display name.
Step 2: Identify the display name for the test method
The display name is the test method name with parentheses, so it prints "Before test: testOne()".
Final Answer:
Before test: testOne() -> Option A
Quick Check:
beforeEach prints test method name [OK]
Hint: BeforeEachCallback runs before each test method [OK]
Common Mistakes:
Thinking it prints class name instead of method
Assuming no output is printed
Confusing interface implementation
4. Consider this extension implementation:
public class FaultyExtension implements BeforeEachCallback {
@Override
public void beforeEach(ExtensionContext context) {
int x = 1 / 0;
}
}
@ExtendWith(FaultyExtension.class)
public class TestClass {
@Test
void testMethod() {}
}
What will happen when running testMethod?
medium
A. The test will be skipped silently
B. The test will fail with an ArithmeticException before running
C. The test will pass normally
D. Compilation error due to division by zero
Solution
Step 1: Analyze the extension code for errors
The extension's beforeEach method divides by zero, causing an ArithmeticException at runtime.
Step 2: Understand effect on test execution
Since the exception occurs before the test runs, the test fails immediately with that exception.
Final Answer:
The test will fail with an ArithmeticException before running -> Option B
Quick Check:
Runtime error in extension causes test failure [OK]
Hint: Runtime errors in extensions fail tests before execution [OK]
Common Mistakes:
Thinking division by zero causes compile error
Assuming test passes despite extension error
Believing test is skipped silently
5. You want to create a custom extension that sets up a database connection before all tests and closes it after all tests in a class. Which two JUnit 5 extension interfaces should you implement together?
hard
A. BeforeEachCallback and AfterEachCallback
B. TestInstancePostProcessor and ParameterResolver
C. BeforeAllCallback and AfterAllCallback
D. BeforeTestExecutionCallback and AfterTestExecutionCallback
Solution
Step 1: Identify lifecycle timing for setup/teardown
Setting up before all tests and closing after all tests requires hooks that run once per test class, not per test method.
Step 2: Match interfaces to lifecycle events
BeforeAllCallback runs before all tests in a class, and AfterAllCallback runs after all tests in a class, perfect for this use case.
Final Answer:
BeforeAllCallback and AfterAllCallback -> Option C