Test Overview
This test checks how a JUnit extension customizes test behavior by adding a setup step before each test and verifying it runs correctly.
This test checks how a JUnit extension customizes test behavior by adding a setup step before each test and verifying it runs correctly.
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertTrue; class CustomSetupExtension implements BeforeEachCallback { boolean setupDone = false; @Override public void beforeEach(ExtensionContext context) { setupDone = true; // Custom setup logic } } public class ExtensionBehaviorTest { @RegisterExtension static CustomSetupExtension extension = new CustomSetupExtension(); @Test void testSetupIsDone() { assertTrue(extension.setupDone, "Setup should be done before test"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit test runner starts the ExtensionBehaviorTest class | Test class loaded, extension registered | — | PASS |
| 2 | JUnit calls beforeEach method of CustomSetupExtension before testSetupIsDone | Extension's setupDone flag set to true | — | PASS |
| 3 | JUnit runs testSetupIsDone method | Test method executing with extension.setupDone == true | assertTrue(extension.setupDone) verifies setupDone is true | PASS |
| 4 | JUnit completes test and reports result | Test passed successfully | — | PASS |
@ExtendWith.@ExtendWith(MyExtension.class)
class MyTest {
@Test
void testMethod() {
System.out.println("Test running");
}
}MyExtension prints "Before test" before the test and "After test" after the test.@ExtendWith(MyExtension.class)
class TestClass {
@Test
void test() {
System.out.println("Running test");
}
void helper() {
System.out.println("Helper method");
}
}helper() not be called during test execution?@Test run as tests; helper() lacks this annotation.BeforeEachCallback interface allows running code before each test method.beforeEach() method is the correct way to customize data before tests.Pass the test and this pattern moves unknown