0
0
JUnittesting~15 mins

@ExtendWith annotation in JUnit - Build an Automation Script

Choose your learning style9 modes available
Automate a JUnit 5 test using @ExtendWith to register a custom extension
Preconditions (2)
Step 1: Create a test class named 'MyTestWithExtension'
Step 2: Annotate the test class with @ExtendWith(MyCustomExtension.class)
Step 3: Write a test method 'testExample' that asserts true is true
Step 4: Run the test class
✅ Expected Result: The test runs successfully with the custom extension applied, and the assertion passes
Automation Requirements - JUnit 5
Assertions Needed:
Assert that the test method executes and passes
Verify that the custom extension is invoked (e.g., by a side effect or log)
Best Practices:
Use @ExtendWith annotation at class level
Keep test methods simple and focused
Use assertions from org.junit.jupiter.api.Assertions
Ensure the custom extension implements the Extension interface
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import static org.junit.jupiter.api.Assertions.assertTrue;

// Custom extension that prints a message before each test
class MyCustomExtension implements BeforeEachCallback {
    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Custom extension before each test method: " + context.getDisplayName());
    }
}

@ExtendWith(MyCustomExtension.class)
public class MyTestWithExtension {

    @Test
    void testExample() {
        assertTrue(true, "True should be true");
    }
}

This code defines a custom extension MyCustomExtension that implements BeforeEachCallback. It prints a message before each test method runs.

The test class MyTestWithExtension is annotated with @ExtendWith(MyCustomExtension.class) to register the extension.

The test method testExample simply asserts true is true, which always passes.

When running the test, the extension's beforeEach method runs first, printing the message, then the test runs and passes.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not annotating the test class or method with @ExtendWith', 'why_bad': "The custom extension will not be registered or executed, so its behavior won't apply.", 'correct_approach': 'Always annotate the test class or specific test methods with @ExtendWith and specify the extension class.'}
{'mistake': 'Using JUnit 4 annotations or imports instead of JUnit 5', 'why_bad': "JUnit 4 does not support @ExtendWith, so the extension mechanism won't work.", 'correct_approach': 'Use JUnit 5 imports and annotations (org.junit.jupiter.api) for extensions.'}
Implementing the extension interface incorrectly or missing required methods
{'mistake': 'Placing @ExtendWith on a method but expecting it to apply to other methods', 'why_bad': "The extension applies only to the annotated method, so other tests won't use it.", 'correct_approach': 'Annotate the whole class if the extension should apply to all test methods.'}
Bonus Challenge

Now add data-driven testing with 3 different inputs to the test method using @ParameterizedTest and @ValueSource

Show Hint