|
11 | 11 | package org.junit.vintage.engine;
|
12 | 12 |
|
13 | 13 | import static org.assertj.core.api.Assertions.assertThat;
|
| 14 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
14 | 15 | import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
|
15 | 16 | import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
|
16 | 17 | import static org.junit.platform.engine.discovery.DiscoverySelectors.selectUniqueId;
|
|
31 | 32 | import static org.junit.runner.Description.createSuiteDescription;
|
32 | 33 | import static org.junit.runner.Description.createTestDescription;
|
33 | 34 |
|
| 35 | +import java.math.BigDecimal; |
| 36 | + |
| 37 | +import junit.runner.Version; |
| 38 | + |
34 | 39 | import org.assertj.core.api.Condition;
|
35 | 40 | import org.junit.AssumptionViolatedException;
|
36 | 41 | import org.junit.jupiter.api.Test;
|
|
80 | 85 | import org.junit.vintage.engine.samples.junit4.JUnit4TestCaseWithRunnerWithDuplicateChangingChildDescriptions;
|
81 | 86 | import org.junit.vintage.engine.samples.junit4.MalformedJUnit4TestCase;
|
82 | 87 | import org.junit.vintage.engine.samples.junit4.ParameterizedTestCase;
|
| 88 | +import org.junit.vintage.engine.samples.junit4.ParameterizedTimingTestCase; |
| 89 | +import org.junit.vintage.engine.samples.junit4.ParameterizedWithAfterParamFailureTestCase; |
| 90 | +import org.junit.vintage.engine.samples.junit4.ParameterizedWithBeforeParamFailureTestCase; |
83 | 91 | import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithFiveTestMethods;
|
84 | 92 | import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithLifecycleMethods;
|
85 | 93 | import org.junit.vintage.engine.samples.junit4.PlainJUnit4TestCaseWithSingleTestWhichFails;
|
@@ -455,6 +463,74 @@ void executesIgnoredParameterizedTestCase() {
|
455 | 463 | event(engine(), finishedSuccessfully()));
|
456 | 464 | }
|
457 | 465 |
|
| 466 | + @Test |
| 467 | + void executesParameterizedTimingTestCase() { |
| 468 | + assumeTrue(atLeastJUnit4_13(), "@BeforeParam and @AfterParam were introduced in JUnit 4.13"); |
| 469 | + |
| 470 | + Class<?> testClass = ParameterizedTimingTestCase.class; |
| 471 | + |
| 472 | + var events = execute(testClass).allEvents().debug(); |
| 473 | + |
| 474 | + var firstParamStartedEvent = events.filter(event(container("[foo]"), started())::matches).findFirst() // |
| 475 | + .orElseThrow(() -> new AssertionError("No start event for [foo]")); |
| 476 | + var firstParamFinishedEvent = events.filter( |
| 477 | + event(container("[foo]"), finishedSuccessfully())::matches).findFirst() // |
| 478 | + .orElseThrow(() -> new AssertionError("No finish event for [foo]")); |
| 479 | + var secondParamStartedEvent = events.filter(event(container("[bar]"), started())::matches).findFirst() // |
| 480 | + .orElseThrow(() -> new AssertionError("No start event for [bar]")); |
| 481 | + var secondParamFinishedEvent = events.filter( |
| 482 | + event(container("[bar]"), finishedSuccessfully())::matches).findFirst() // |
| 483 | + .orElseThrow(() -> new AssertionError("No finish event for [bar]")); |
| 484 | + |
| 485 | + assertThat(ParameterizedTimingTestCase.EVENTS.get("beforeParam(foo)")).isAfterOrEqualTo( |
| 486 | + firstParamStartedEvent.getTimestamp()); |
| 487 | + assertThat(ParameterizedTimingTestCase.EVENTS.get("afterParam(foo)")).isBeforeOrEqualTo( |
| 488 | + firstParamFinishedEvent.getTimestamp()); |
| 489 | + assertThat(ParameterizedTimingTestCase.EVENTS.get("beforeParam(bar)")).isAfterOrEqualTo( |
| 490 | + secondParamStartedEvent.getTimestamp()); |
| 491 | + assertThat(ParameterizedTimingTestCase.EVENTS.get("afterParam(bar)")).isBeforeOrEqualTo( |
| 492 | + secondParamFinishedEvent.getTimestamp()); |
| 493 | + } |
| 494 | + |
| 495 | + @Test |
| 496 | + void executesParameterizedWithAfterParamFailureTestCase() { |
| 497 | + assumeTrue(atLeastJUnit4_13(), "@AfterParam was introduced in JUnit 4.13"); |
| 498 | + |
| 499 | + Class<?> testClass = ParameterizedWithAfterParamFailureTestCase.class; |
| 500 | + |
| 501 | + execute(testClass).allEvents().assertEventsMatchExactly( // |
| 502 | + event(engine(), started()), // |
| 503 | + event(container(testClass), started()), // |
| 504 | + event(container("[foo]"), started()), // |
| 505 | + event(test("test[foo]"), started()), // |
| 506 | + event(test("test[foo]"), finishedSuccessfully()), // |
| 507 | + event(container("[foo]"), finishedWithFailure(instanceOf(AssertionError.class))), // |
| 508 | + event(container("[bar]"), started()), // |
| 509 | + event(test("test[bar]"), started()), // |
| 510 | + event(test("test[bar]"), |
| 511 | + finishedWithFailure(instanceOf(AssertionError.class), message("expected:<[foo]> but was:<[bar]>"))), // |
| 512 | + event(container("[bar]"), finishedWithFailure(instanceOf(AssertionError.class))), // |
| 513 | + event(container(testClass), finishedSuccessfully()), // |
| 514 | + event(engine(), finishedSuccessfully())); |
| 515 | + } |
| 516 | + |
| 517 | + @Test |
| 518 | + void executesParameterizedWithBeforeParamFailureTestCase() { |
| 519 | + assumeTrue(atLeastJUnit4_13(), "@BeforeParam was introduced in JUnit 4.13"); |
| 520 | + |
| 521 | + Class<?> testClass = ParameterizedWithBeforeParamFailureTestCase.class; |
| 522 | + |
| 523 | + execute(testClass).allEvents().assertEventsMatchExactly( // |
| 524 | + event(engine(), started()), // |
| 525 | + event(container(testClass), started()), // |
| 526 | + event(container("[foo]"), started()), // |
| 527 | + event(container("[foo]"), finishedWithFailure(instanceOf(AssertionError.class))), // |
| 528 | + event(container("[bar]"), started()), // |
| 529 | + event(container("[bar]"), finishedWithFailure(instanceOf(AssertionError.class))), // |
| 530 | + event(container(testClass), finishedSuccessfully()), // |
| 531 | + event(engine(), finishedSuccessfully())); |
| 532 | + } |
| 533 | + |
458 | 534 | @Test
|
459 | 535 | void executesJUnit4TestCaseWithExceptionThrowingRunner() {
|
460 | 536 | Class<?> testClass = JUnit4TestCaseWithExceptionThrowingRunner.class;
|
@@ -806,4 +882,8 @@ private static LauncherDiscoveryRequest request(Class<?> testClass) {
|
806 | 882 | return LauncherDiscoveryRequestBuilder.request().selectors(selectClass(testClass)).build();
|
807 | 883 | }
|
808 | 884 |
|
| 885 | + private static boolean atLeastJUnit4_13() { |
| 886 | + return JUnit4VersionCheck.parseVersion(Version.id()).compareTo(new BigDecimal("4.13")) >= 0; |
| 887 | + } |
| 888 | + |
809 | 889 | }
|
0 commit comments