Skip to content

Commit 50cc23c

Browse files
committed
Use the JUnit Platform Test Kit in spring-test
1 parent 43119de commit 50cc23c

File tree

2 files changed

+39
-72
lines changed

2 files changed

+39
-72
lines changed

spring-test/spring-test.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ dependencies {
6262
testCompile("javax.mail:javax.mail-api:1.6.2")
6363
testCompile("org.hibernate:hibernate-core:5.4.2.Final")
6464
testCompile("org.hibernate:hibernate-validator:6.0.16.Final")
65-
// Enable use of the JUnit Platform Runner
6665
testCompile("org.junit.platform:junit-platform-runner")
66+
testCompile("org.junit.platform:junit-platform-testkit")
6767
testCompile("org.junit.jupiter:junit-jupiter-params")
6868
testCompile("com.fasterxml.jackson.core:jackson-databind:${jackson2Version}")
6969
testCompile("com.thoughtworks.xstream:xstream:1.4.10")

spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java

Lines changed: 38 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,14 @@
1616

1717
package org.springframework.test.context.junit.jupiter;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
import java.util.stream.Stream;
2219
import javax.sql.DataSource;
2320

24-
import org.junit.jupiter.api.DynamicTest;
2521
import org.junit.jupiter.api.Test;
26-
import org.junit.jupiter.api.TestFactory;
2722
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;
3427

3528
import org.springframework.context.annotation.Bean;
3629
import org.springframework.context.annotation.Configuration;
@@ -45,12 +38,13 @@
4538
import org.springframework.transaction.PlatformTransactionManager;
4639
import org.springframework.transaction.annotation.Transactional;
4740

48-
import static org.assertj.core.api.Assertions.assertThat;
4941
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;
5242
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;
5448

5549
/**
5650
* Integration tests which verify that '<i>before</i>' and '<i>after</i>'
@@ -72,53 +66,38 @@
7266
*/
7367
class FailingBeforeAndAfterMethodsSpringExtensionTests {
7468

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)));
11692

11793
// Ensure it was an AssertionError that failed the test and not
11894
// 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")))));
122101
}
123102
}
124103

@@ -279,16 +258,4 @@ DataSource dataSource() {
279258
}
280259
}
281260

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-
294261
}

0 commit comments

Comments
 (0)