|
| 1 | +package io.vertx.junit5.tests; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import org.junit.platform.engine.DiscoverySelector; |
| 5 | +import org.junit.platform.engine.TestExecutionResult; |
| 6 | +import org.junit.platform.engine.discovery.DiscoverySelectors; |
| 7 | +import org.junit.platform.engine.support.descriptor.ClassSource; |
| 8 | +import org.junit.platform.launcher.EngineFilter; |
| 9 | +import org.junit.platform.launcher.Launcher; |
| 10 | +import org.junit.platform.launcher.LauncherDiscoveryRequest; |
| 11 | +import org.junit.platform.launcher.TestExecutionListener; |
| 12 | +import org.junit.platform.launcher.TestIdentifier; |
| 13 | +import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder; |
| 14 | +import org.junit.platform.launcher.core.LauncherFactory; |
| 15 | +import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
| 16 | +import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | + |
| 22 | +class RunAfterEachContextCheckTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + void runsWaitForContextInAfterEachMethodTestAndChecksAfterAllSucceeded() { |
| 26 | + // Select only the target class |
| 27 | + final DiscoverySelector selector = DiscoverySelectors.selectClass(WaitForContextInAfterEachMethodTest.class); |
| 28 | + |
| 29 | + // Collect a summary for assertions |
| 30 | + final SummaryGeneratingListener summaryListener = new SummaryGeneratingListener(); |
| 31 | + |
| 32 | + // Capture the class container result specifically (to assert @AfterAll behavior) |
| 33 | + final AtomicReference<TestExecutionResult.Status> classStatus = new AtomicReference<>(); |
| 34 | + |
| 35 | + final TestExecutionListener captureClassStatus = new TestExecutionListener() { |
| 36 | + @Override |
| 37 | + public void executionFinished(final TestIdentifier id, final TestExecutionResult result) { |
| 38 | + id.getSource() |
| 39 | + .ifPresent(source -> { |
| 40 | + if (id.isContainer() && source instanceof ClassSource) { |
| 41 | + final ClassSource cs = (ClassSource) source; |
| 42 | + if (cs.getClassName().equals(WaitForContextInAfterEachMethodTest.class.getName())) { |
| 43 | + classStatus.set(result.getStatus()); |
| 44 | + } |
| 45 | + } |
| 46 | + }); |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + final LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request() |
| 51 | + .selectors(selector) |
| 52 | + .filters(EngineFilter.includeEngines("junit-jupiter")) |
| 53 | + // Make @Disabled inert for this run |
| 54 | + .configurationParameter( |
| 55 | + "junit.jupiter.conditions.deactivate", "org.junit.*DisabledCondition") |
| 56 | + // Make execution deterministic |
| 57 | + .configurationParameter("junit.jupiter.execution.parallel.enabled", "false") |
| 58 | + .build(); |
| 59 | + |
| 60 | + final Launcher launcher = LauncherFactory.create(); |
| 61 | + launcher.registerTestExecutionListeners(summaryListener, captureClassStatus); |
| 62 | + |
| 63 | + launcher.execute(request); |
| 64 | + |
| 65 | + final TestExecutionSummary summary = summaryListener.getSummary(); |
| 66 | + |
| 67 | + // The single test method intentionally fails |
| 68 | + assertEquals(1, summary.getTestsFoundCount(), "Expect exactly one test discovered"); |
| 69 | + assertEquals(1, summary.getTestsFailedCount(), "The test should fail via context.failNow()"); |
| 70 | + assertEquals(0, summary.getContainersFailedCount(), "The test class container should not fail"); |
| 71 | + |
| 72 | + // Critically: the class container (where @AfterAll runs) must be SUCCESSFUL |
| 73 | + assertEquals(TestExecutionResult.Status.SUCCESSFUL, |
| 74 | + classStatus.get(), |
| 75 | + "@AfterAll must complete its VertxTestContext without failure"); |
| 76 | + } |
| 77 | +} |
0 commit comments