0
0
JUnittesting~15 mins

Custom extensions in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify custom JUnit extension logs test start and end
Preconditions (2)
Step 1: Create a test class and register the custom extension using @ExtendWith
Step 2: Write a simple test method that asserts true is true
Step 3: Run the test class
Step 4: Observe the console output for logs from the custom extension indicating test start and test end
✅ Expected Result: The console shows log messages from the custom extension before and after the test method runs, and the test passes
Automation Requirements - JUnit 5
Assertions Needed:
Assert that the test method passes successfully
Verify via console output that the extension logs test start and end
Best Practices:
Use @ExtendWith annotation to register the extension
Implement BeforeTestExecutionCallback and AfterTestExecutionCallback interfaces in the extension
Keep test methods simple and focused
Use standard JUnit assertions
Automated Solution
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.AfterTestExecutionCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

import static org.junit.jupiter.api.Assertions.assertTrue;

class LoggingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback {
    @Override
    public void beforeTestExecution(ExtensionContext context) {
        System.out.println("[Extension] Starting test: " + context.getDisplayName());
    }

    @Override
    public void afterTestExecution(ExtensionContext context) {
        System.out.println("[Extension] Finished test: " + context.getDisplayName());
    }
}

@ExtendWith(LoggingExtension.class)
public class CustomExtensionTest {

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

The LoggingExtension class implements two JUnit 5 extension interfaces: BeforeTestExecutionCallback and AfterTestExecutionCallback. These allow it to run code before and after each test method.

Inside beforeTestExecution, it prints a message indicating the test is starting. Inside afterTestExecution, it prints a message indicating the test finished.

The test class CustomExtensionTest uses @ExtendWith(LoggingExtension.class) to register the extension. It has one simple test method simpleTest that asserts true is true.

When running the test, the console will show the extension's log messages before and after the test method runs, confirming the extension works. The test assertion ensures the test passes.

Common Mistakes - 3 Pitfalls
Not registering the extension with @ExtendWith annotation
Implementing the wrong extension interfaces
Using System.out.println in production extensions without proper logging
Bonus Challenge

Modify the custom extension to measure and log the duration of each test execution

Show Hint