0
0
JUnittesting~10 mins

BeforeEachCallback and AfterEachCallback in JUnit - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how BeforeEachCallback and AfterEachCallback work in JUnit 5. It verifies that setup code runs before each test and cleanup code runs after each test.

Test Code - JUnit 5
JUnit
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class LifecycleExtensionTest {

    static class MyExtension implements BeforeEachCallback, AfterEachCallback {
        boolean beforeCalled = false;
        boolean afterCalled = false;

        @Override
        public void beforeEach(ExtensionContext context) {
            beforeCalled = true;
            System.out.println("BeforeEachCallback executed");
        }

        @Override
        public void afterEach(ExtensionContext context) {
            afterCalled = true;
            System.out.println("AfterEachCallback executed");
        }
    }

    @RegisterExtension
    static MyExtension extension = new MyExtension();

    @Test
    void testOne() {
        assertTrue(extension.beforeCalled, "BeforeEachCallback should have run before testOne");
        extension.beforeCalled = false; // reset for next test
    }

    @Test
    void testTwo() {
        assertTrue(extension.beforeCalled, "BeforeEachCallback should have run before testTwo");
        extension.beforeCalled = false; // reset for next test
    }

}
Execution Trace - 10 Steps
StepActionSystem StateAssertionResult
1JUnit starts testOneTest lifecycle begins for testOne-PASS
2MyExtension.beforeEach() runsbeforeCalled flag set to true-PASS
3testOne executes assertion on beforeCalledbeforeCalled is trueassertTrue(extension.beforeCalled)PASS
4testOne resets beforeCalled to falsebeforeCalled reset-PASS
5MyExtension.afterEach() runs after testOneafterCalled flag set to true-PASS
6JUnit starts testTwoTest lifecycle begins for testTwo-PASS
7MyExtension.beforeEach() runsbeforeCalled flag set to true-PASS
8testTwo executes assertion on beforeCalledbeforeCalled is trueassertTrue(extension.beforeCalled)PASS
9testTwo resets beforeCalled to falsebeforeCalled reset-PASS
10MyExtension.afterEach() runs after testTwoafterCalled flag set to true-PASS
Failure Scenario
Failing Condition: MyExtension.beforeEach() does not set beforeCalled to true before test runs
Execution Trace Quiz - 3 Questions
Test your understanding
What does BeforeEachCallback do in this test?
ARuns only once before all tests
BRuns setup code before each test method
CRuns cleanup code after all tests
DSkips the test if condition fails
Key Result
Using BeforeEachCallback and AfterEachCallback helps run setup and cleanup code automatically around each test, improving test reliability and reducing repeated code.