0
0
JUnittesting~15 mins

Extension model overview in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify custom extension execution in JUnit 5
Preconditions (2)
Step 1: Create a test class and register the custom extension using @ExtendWith annotation
Step 2: Add a test method that performs a simple assertion
Step 3: Run the test class
✅ Expected Result: The custom extension's beforeEach method is executed before the test method, and the test passes
Automation Requirements - JUnit 5
Assertions Needed:
Verify the test method runs successfully
Verify the extension's beforeEach callback is invoked
Best Practices:
Use @ExtendWith to register extensions
Implement BeforeEachCallback interface for setup logic
Use assertions from org.junit.jupiter.api.Assertions
Keep extension logic simple and focused
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtendWith;
import static org.junit.jupiter.api.Assertions.assertTrue;

class CustomExtension implements BeforeEachCallback {
    static boolean beforeEachCalled = false;

    @Override
    public void beforeEach(ExtensionContext context) {
        beforeEachCalled = true;
        System.out.println("CustomExtension beforeEach called");
    }
}

@ExtendWith(CustomExtension.class)
public class ExtensionModelTest {

    @Test
    void testWithExtension() {
        assertTrue(CustomExtension.beforeEachCalled, "Extension beforeEach should be called before test");
    }
}

The CustomExtension class implements BeforeEachCallback to run code before each test method.

We use a static boolean beforeEachCalled to track if the extension method runs.

The test class ExtensionModelTest registers the extension with @ExtendWith.

The test method asserts that the extension's beforeEach method was called before the test runs.

This shows how JUnit 5 extensions can add behavior around tests.

Common Mistakes - 3 Pitfalls
{'mistake': 'Not registering the extension with @ExtendWith annotation', 'why_bad': "The extension code will never run if not registered, so tests won't have the expected behavior.", 'correct_approach': 'Always use @ExtendWith on the test class or method to activate the extension.'}
{'mistake': 'Using instance variables in the extension to track state', 'why_bad': "JUnit creates a new extension instance per test, so instance variables won't keep state across tests.", 'correct_approach': 'Use static variables or external storage if you need to track state across tests.'}
{'mistake': 'Not implementing the correct callback interface for the desired lifecycle event', 'why_bad': "The extension method won't be called if the interface doesn't match the lifecycle event you want to hook into.", 'correct_approach': 'Implement the appropriate interface like BeforeEachCallback, AfterEachCallback, etc., depending on when you want your code to run.'}
Bonus Challenge

Now add a second extension that implements AfterEachCallback and verify it runs after the test method

Show Hint