Challenge - 5 Problems
JUnit Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
Purpose of BeforeEachCallback in JUnit 5
What is the main purpose of implementing the BeforeEachCallback interface in a JUnit 5 extension?
Attempts:
2 left
💡 Hint
Think about when you want to prepare the test environment for every test method.
✗ Incorrect
BeforeEachCallback runs code before each test method, allowing setup specific to each test.
🧠 Conceptual
intermediate1:30remaining
Difference between BeforeEachCallback and AfterEachCallback
Which statement correctly describes the difference between BeforeEachCallback and AfterEachCallback in JUnit 5?
Attempts:
2 left
💡 Hint
Think about setup and cleanup around each test.
✗ Incorrect
BeforeEachCallback runs before each test method; AfterEachCallback runs after each test method.
❓ Predict Output
advanced2:00remaining
Output of JUnit Extension with BeforeEachCallback and AfterEachCallback
Given the following JUnit 5 extension code, what will be printed to the console when running a test method using this extension?
JUnit
import org.junit.jupiter.api.extension.*; public class MyExtension implements BeforeEachCallback, AfterEachCallback { @Override public void beforeEach(ExtensionContext context) { System.out.println("Setup before test"); } @Override public void afterEach(ExtensionContext context) { System.out.println("Cleanup after test"); } } // Test class uses @ExtendWith(MyExtension.class) and has one test method that prints "Running test"
Attempts:
2 left
💡 Hint
Remember the order: beforeEach runs before the test, afterEach runs after.
✗ Incorrect
The extension prints before the test method runs, then the test prints, then afterEach prints.
🔧 Debug
advanced2:00remaining
Identify the error in this AfterEachCallback implementation
What error will occur when running this JUnit 5 extension code implementing AfterEachCallback?
JUnit
import org.junit.jupiter.api.extension.*; public class FaultyExtension implements AfterEachCallback { @Override public void afterEach() { System.out.println("Cleaning up"); } }
Attempts:
2 left
💡 Hint
Check the method signature required by AfterEachCallback interface.
✗ Incorrect
The afterEach method must accept an ExtensionContext parameter; missing it causes a compile error.
❓ framework
expert2:30remaining
Correct usage of BeforeEachCallback and AfterEachCallback in a JUnit 5 extension
Which code snippet correctly implements both BeforeEachCallback and AfterEachCallback interfaces in a JUnit 5 extension to print messages before and after each test method?
Attempts:
2 left
💡 Hint
Check method signatures and use of @Override annotations.
✗ Incorrect
Correct implementation requires method signatures with ExtensionContext parameter and @Override annotations.