0
0
JUnittesting~5 mins

BeforeEachCallback and AfterEachCallback in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the BeforeEachCallback interface in JUnit?
It allows you to run custom code before each test method is executed, such as setting up test data or initializing resources.
Click to reveal answer
beginner
What does the AfterEachCallback interface do in JUnit?
It lets you run custom code after each test method finishes, useful for cleaning up resources or resetting states.
Click to reveal answer
intermediate
How do BeforeEachCallback and AfterEachCallback relate to test lifecycle?
They hook into the test lifecycle to run code right before and right after each test method, helping manage setup and cleanup automatically.
Click to reveal answer
beginner
Which method must be implemented when using BeforeEachCallback?
You must implement the beforeEach(ExtensionContext context) method to define what happens before each test.
Click to reveal answer
intermediate
What parameter is passed to the afterEach method in AfterEachCallback?
An ExtensionContext object, which provides information about the current test and its environment.
Click to reveal answer
What is the main use of BeforeEachCallback in JUnit?
ARun code before each test method
BRun code after all tests
CSkip a test method
DRun code only once before all tests
Which method must you override when implementing AfterEachCallback?
AafterEach()
BafterAll()
CbeforeAll()
DbeforeEach()
What does the ExtensionContext parameter provide in these callbacks?
AUser input data
BDatabase connection
CTest report generation
DTest method details and environment info
When is AfterEachCallback code executed?
ABefore each test method
BAfter each test method
CBefore all tests
DAfter all tests
Can BeforeEachCallback and AfterEachCallback be used together?
AYes, but only in JUnit 4
BNo, only one can be used
CYes, to run setup and cleanup code around each test
DNo, they serve the same purpose
Explain how BeforeEachCallback and AfterEachCallback help manage test setup and cleanup in JUnit.
Think about what happens before and after each test method.
You got /4 concepts.
    Describe the role of the ExtensionContext parameter in the methods of BeforeEachCallback and AfterEachCallback.
    It helps the callback know about the test it is running around.
    You got /3 concepts.