|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.junit.jupiter;
|
18 | 18 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.List; |
21 |
| -import java.util.stream.Stream; |
22 | 19 | import javax.sql.DataSource;
|
23 | 20 |
|
24 |
| -import org.junit.jupiter.api.DynamicTest; |
25 | 21 | import org.junit.jupiter.api.Test;
|
26 |
| -import org.junit.jupiter.api.TestFactory; |
27 | 22 | import org.junit.jupiter.api.extension.ExtendWith;
|
28 |
| -import org.junit.platform.engine.TestExecutionResult; |
29 |
| -import org.junit.platform.launcher.Launcher; |
30 |
| -import org.junit.platform.launcher.TestIdentifier; |
31 |
| -import org.junit.platform.launcher.core.LauncherFactory; |
32 |
| -import org.junit.platform.launcher.listeners.SummaryGeneratingListener; |
33 |
| -import org.junit.platform.launcher.listeners.TestExecutionSummary; |
| 23 | +import org.junit.jupiter.params.ParameterizedTest; |
| 24 | +import org.junit.jupiter.params.provider.ValueSource; |
| 25 | +import org.junit.platform.testkit.engine.EngineTestKit; |
| 26 | +import org.junit.platform.testkit.engine.Events; |
34 | 27 |
|
35 | 28 | import org.springframework.context.annotation.Bean;
|
36 | 29 | import org.springframework.context.annotation.Configuration;
|
|
45 | 38 | import org.springframework.transaction.PlatformTransactionManager;
|
46 | 39 | import org.springframework.transaction.annotation.Transactional;
|
47 | 40 |
|
48 |
| -import static org.assertj.core.api.Assertions.assertThat; |
49 | 41 | import static org.assertj.core.api.Assertions.fail;
|
50 |
| -import static org.assertj.core.api.SoftAssertions.assertSoftly; |
51 |
| -import static org.junit.jupiter.api.DynamicTest.dynamicTest; |
52 | 42 | import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
|
53 |
| -import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; |
| 43 | +import static org.junit.platform.testkit.engine.EventConditions.event; |
| 44 | +import static org.junit.platform.testkit.engine.EventConditions.finishedWithFailure; |
| 45 | +import static org.junit.platform.testkit.engine.EventConditions.test; |
| 46 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.instanceOf; |
| 47 | +import static org.junit.platform.testkit.engine.TestExecutionResultConditions.message; |
54 | 48 |
|
55 | 49 | /**
|
56 | 50 | * Integration tests which verify that '<i>before</i>' and '<i>after</i>'
|
|
72 | 66 | */
|
73 | 67 | class FailingBeforeAndAfterMethodsSpringExtensionTests {
|
74 | 68 |
|
75 |
| - private static Stream<Class<?>> testClasses() { |
76 |
| - // @formatter:off |
77 |
| - return Stream.of( |
78 |
| - AlwaysFailingBeforeTestClassTestCase.class, |
79 |
| - AlwaysFailingAfterTestClassTestCase.class, |
80 |
| - AlwaysFailingPrepareTestInstanceTestCase.class, |
81 |
| - AlwaysFailingBeforeTestMethodTestCase.class, |
82 |
| - AlwaysFailingBeforeTestExecutionTestCase.class, |
83 |
| - AlwaysFailingAfterTestExecutionTestCase.class, |
84 |
| - AlwaysFailingAfterTestMethodTestCase.class, |
85 |
| - FailingBeforeTransactionTestCase.class, |
86 |
| - FailingAfterTransactionTestCase.class); |
87 |
| - // @formatter:on |
88 |
| - } |
89 |
| - |
90 |
| - @TestFactory |
91 |
| - Stream<DynamicTest> generateTests() throws Exception { |
92 |
| - return testClasses().map(clazz -> dynamicTest(clazz.getSimpleName(), () -> runTestAndAssertCounters(clazz))); |
93 |
| - } |
94 |
| - |
95 |
| - private void runTestAndAssertCounters(Class<?> testClass) { |
96 |
| - Launcher launcher = LauncherFactory.create(); |
97 |
| - ExceptionTrackingListener listener = new ExceptionTrackingListener(); |
98 |
| - launcher.registerTestExecutionListeners(listener); |
99 |
| - |
100 |
| - launcher.execute(request().selectors(selectClass(testClass)).build()); |
101 |
| - TestExecutionSummary summary = listener.getSummary(); |
102 |
| - |
103 |
| - String name = testClass.getSimpleName(); |
104 |
| - int expectedStartedCount = getExpectedStartedCount(testClass); |
105 |
| - int expectedSucceededCount = getExpectedSucceededCount(testClass); |
106 |
| - int expectedFailedCount = getExpectedFailedCount(testClass); |
107 |
| - |
108 |
| - assertSoftly(softly -> { |
109 |
| - softly.assertThat(summary.getTestsFoundCount()).as("%s: tests found", name).isEqualTo(1); |
110 |
| - softly.assertThat(summary.getTestsSkippedCount()).as("%s: tests skipped", name).isEqualTo(0); |
111 |
| - softly.assertThat(summary.getTestsAbortedCount()).as("%s: tests aborted", name).isEqualTo(0); |
112 |
| - softly.assertThat(summary.getTestsStartedCount()).as("%s: tests started", name).isEqualTo(expectedStartedCount); |
113 |
| - softly.assertThat(summary.getTestsSucceededCount()).as("%s: tests succeeded", name).isEqualTo(expectedSucceededCount); |
114 |
| - softly.assertThat(summary.getTestsFailedCount()).as("%s: tests failed", name).isEqualTo(expectedFailedCount); |
115 |
| - }); |
| 69 | + @ParameterizedTest |
| 70 | + @ValueSource(classes = { |
| 71 | + AlwaysFailingBeforeTestClassTestCase.class, |
| 72 | + AlwaysFailingAfterTestClassTestCase.class, |
| 73 | + AlwaysFailingPrepareTestInstanceTestCase.class, |
| 74 | + AlwaysFailingBeforeTestMethodTestCase.class, |
| 75 | + AlwaysFailingBeforeTestExecutionTestCase.class, |
| 76 | + AlwaysFailingAfterTestExecutionTestCase.class, |
| 77 | + AlwaysFailingAfterTestMethodTestCase.class, |
| 78 | + FailingBeforeTransactionTestCase.class, |
| 79 | + FailingAfterTransactionTestCase.class |
| 80 | + }) |
| 81 | + void failingBeforeAndAfterCallbacks(Class<?> testClass) { |
| 82 | + Events events = EngineTestKit.engine("junit-jupiter") |
| 83 | + .selectors(selectClass(testClass)) |
| 84 | + .execute() |
| 85 | + .tests() |
| 86 | + .assertStatistics(stats -> stats |
| 87 | + .skipped(0) |
| 88 | + .aborted(0) |
| 89 | + .started(getExpectedStartedCount(testClass)) |
| 90 | + .succeeded(getExpectedSucceededCount(testClass)) |
| 91 | + .failed(getExpectedFailedCount(testClass))); |
116 | 92 |
|
117 | 93 | // Ensure it was an AssertionError that failed the test and not
|
118 | 94 | // something else like an error in the @Configuration class, etc.
|
119 |
| - if (expectedFailedCount > 0) { |
120 |
| - assertThat(listener.exceptions).as("exceptions expected").hasSize(1); |
121 |
| - assertThat(listener.exceptions.get(0)).isInstanceOf(AssertionError.class); |
| 95 | + if (getExpectedFailedCount(testClass) > 0) { |
| 96 | + events.assertThatEvents().haveExactly(1, |
| 97 | + event(test("testNothing"), |
| 98 | + finishedWithFailure( |
| 99 | + instanceOf(AssertionError.class), |
| 100 | + message(msg -> msg.contains("always failing"))))); |
122 | 101 | }
|
123 | 102 | }
|
124 | 103 |
|
@@ -279,16 +258,4 @@ DataSource dataSource() {
|
279 | 258 | }
|
280 | 259 | }
|
281 | 260 |
|
282 |
| - private static class ExceptionTrackingListener extends SummaryGeneratingListener { |
283 |
| - |
284 |
| - List<Throwable> exceptions = new ArrayList<>(); |
285 |
| - |
286 |
| - |
287 |
| - @Override |
288 |
| - public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { |
289 |
| - super.executionFinished(testIdentifier, testExecutionResult); |
290 |
| - testExecutionResult.getThrowable().ifPresent(exceptions::add); |
291 |
| - } |
292 |
| - } |
293 |
| - |
294 | 261 | }
|
0 commit comments