Test Overview
This test demonstrates how to use JUnit 5 extension model to log test execution start and end. It verifies that the extension methods are called during the test lifecycle.
This test demonstrates how to use JUnit 5 extension model to log test execution start and end. It verifies that the extension methods are called during the test lifecycle.
import org.junit.jupiter.api.extension.*; import org.junit.jupiter.api.*; class LoggingExtension implements BeforeTestExecutionCallback, AfterTestExecutionCallback { @Override public void beforeTestExecution(ExtensionContext context) { System.out.println("Starting test: " + context.getDisplayName()); } @Override public void afterTestExecution(ExtensionContext context) { System.out.println("Finished test: " + context.getDisplayName()); } } @ExtendWith(LoggingExtension.class) public class ExtensionModelTest { @Test void sampleTest() { Assertions.assertTrue(1 + 1 == 2); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit test runner starts the test suite | JUnit framework initialized, test class ExtensionModelTest loaded | - | PASS |
| 2 | JUnit detects @ExtendWith annotation and registers LoggingExtension | LoggingExtension instance ready to intercept test lifecycle events | - | PASS |
| 3 | JUnit calls LoggingExtension.beforeTestExecution before sampleTest runs | Console prints: "Starting test: sampleTest()" | Verify beforeTestExecution callback is invoked | PASS |
| 4 | JUnit executes sampleTest method | Test method runs, assertion 1 + 1 == 2 evaluated | AssertTrue passes as 1 + 1 equals 2 | PASS |
| 5 | JUnit calls LoggingExtension.afterTestExecution after sampleTest completes | Console prints: "Finished test: sampleTest()" | Verify afterTestExecution callback is invoked | PASS |
| 6 | JUnit test runner completes the test suite | Test report generated showing sampleTest passed | Test result is PASS | PASS |