0
0
JUnittesting~15 mins

BeforeEachCallback and AfterEachCallback in JUnit - Build an Automation Script

Choose your learning style9 modes available
Automate setup and cleanup using BeforeEachCallback and AfterEachCallback in JUnit
Preconditions (2)
Step 1: Create a class that implements BeforeEachCallback and AfterEachCallback interfaces
Step 2: In the beforeEach method, add code to print 'Setup before each test'
Step 3: In the afterEach method, add code to print 'Cleanup after each test'
Step 4: Register this callback class as an extension in the test class using @ExtendWith
Step 5: Write two simple test methods that assert basic conditions
Step 6: Run the tests and observe the console output for setup and cleanup messages before and after each test
✅ Expected Result: Each test method runs successfully, and console output shows 'Setup before each test' before each test and 'Cleanup after each test' after each test
Automation Requirements - JUnit 5
Assertions Needed:
Verify that test methods pass
Verify that BeforeEachCallback and AfterEachCallback methods are invoked for each test
Best Practices:
Use @ExtendWith to register extensions
Keep setup and cleanup code separate from test logic
Use descriptive method names
Avoid side effects in callbacks
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
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.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SetupCleanupExtension implements BeforeEachCallback, AfterEachCallback {
    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Setup before each test");
    }

    @Override
    public void afterEach(ExtensionContext context) {
        System.out.println("Cleanup after each test");
    }
}

@ExtendWith(SetupCleanupExtension.class)
public class SampleTest {

    @Test
    void testOne() {
        assertTrue(1 + 1 == 2);
    }

    @Test
    void testTwo() {
        assertTrue("hello".startsWith("h"));
    }
}

This code defines a class SetupCleanupExtension that implements BeforeEachCallback and AfterEachCallback. The beforeEach method prints a setup message before each test, and the afterEach method prints a cleanup message after each test.

The test class SampleTest uses @ExtendWith to register this extension. It contains two simple test methods with assertions that always pass.

When running the tests, the console will show the setup message before each test and the cleanup message after each test, demonstrating the callbacks work as expected.

Common Mistakes - 3 Pitfalls
Not registering the extension with @ExtendWith
Putting test logic inside the callback methods
Using System.out.println for important test verification
Bonus Challenge

Now add a data-driven test that runs the same test logic with three different input values using @ParameterizedTest

Show Hint