Skip to content

Commit ed86daa

Browse files
committed
Convert Spring @⁠Inject TCK tests from JUnit 3 to JUnit Jupiter
Closes gh-35126
1 parent ad278cd commit ed86daa

File tree

2 files changed

+69
-14
lines changed

2 files changed

+69
-14
lines changed

spring-context/spring-context.gradle

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,4 @@ dependencies {
5656
// Substitute for javax.management:jmxremote_optional:1.0.1_04 (not available on Maven Central)
5757
testRuntimeOnly("org.glassfish.external:opendmk_jmxremote_optional_jar")
5858
testRuntimeOnly("org.javamoney:moneta")
59-
testRuntimeOnly("org.junit.vintage:junit-vintage-engine") // for @Inject TCK
60-
}
61-
62-
test {
63-
description = "Runs JUnit Jupiter tests and the @Inject TCK via JUnit Vintage."
64-
useJUnitPlatform {
65-
includeEngines "junit-jupiter", "junit-vintage"
66-
}
6759
}

spring-context/src/test/java/org/springframework/context/annotation/jsr330/SpringAtInjectTckTests.java

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616

1717
package org.springframework.context.annotation.jsr330;
1818

19-
import junit.framework.Test;
19+
import java.util.Enumeration;
20+
import java.util.Spliterator;
21+
import java.util.Spliterators;
22+
import java.util.stream.Stream;
23+
import java.util.stream.StreamSupport;
24+
25+
import junit.framework.TestCase;
26+
import junit.framework.TestFailure;
27+
import junit.framework.TestResult;
28+
import junit.framework.TestSuite;
2029
import org.atinject.tck.Tck;
2130
import org.atinject.tck.auto.Car;
2231
import org.atinject.tck.auto.Convertible;
@@ -28,21 +37,36 @@
2837
import org.atinject.tck.auto.V8Engine;
2938
import org.atinject.tck.auto.accessories.Cupholder;
3039
import org.atinject.tck.auto.accessories.SpareTire;
40+
import org.junit.jupiter.api.DynamicNode;
41+
import org.junit.jupiter.api.TestFactory;
3142

3243
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
3344
import org.springframework.context.annotation.Jsr330ScopeMetadataResolver;
3445
import org.springframework.context.annotation.Primary;
3546
import org.springframework.context.support.GenericApplicationContext;
3647

48+
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
49+
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
50+
3751
/**
52+
* {@code @Inject} Technology Compatibility Kit (TCK) tests.
53+
*
3854
* @author Juergen Hoeller
55+
* @author Sam Brannen
3956
* @since 3.0
57+
* @see org.atinject.tck.Tck
4058
*/
41-
// WARNING: This class MUST be public, since it is based on JUnit 3.
42-
public class SpringAtInjectTckTests {
59+
class SpringAtInjectTckTests {
60+
61+
@TestFactory
62+
Stream<? extends DynamicNode> runTechnologyCompatibilityKit() {
63+
TestSuite testSuite = (TestSuite) Tck.testsFor(buildCar(), false, true);
64+
return generateDynamicTests(testSuite);
65+
}
66+
4367

4468
@SuppressWarnings("unchecked")
45-
public static Test suite() {
69+
private static Car buildCar() {
4670
GenericApplicationContext ac = new GenericApplicationContext();
4771
AnnotatedBeanDefinitionReader bdr = new AnnotatedBeanDefinitionReader(ac);
4872
bdr.setScopeMetadataResolver(new Jsr330ScopeMetadataResolver());
@@ -57,9 +81,48 @@ public static Test suite() {
5781
bdr.registerBean(FuelTank.class);
5882

5983
ac.refresh();
60-
Car car = ac.getBean(Car.class);
84+
return ac.getBean(Car.class);
85+
}
86+
87+
private static Stream<? extends DynamicNode> generateDynamicTests(TestSuite testSuite) {
88+
return stream(testSuite.tests()).map(test -> {
89+
if (test instanceof TestSuite nestedSuite) {
90+
return dynamicContainer(nestedSuite.getName(), generateDynamicTests(nestedSuite));
91+
}
92+
if (test instanceof TestCase testCase) {
93+
return dynamicTest(testCase.getName(), () -> runTestCase(testCase));
94+
}
95+
throw new IllegalStateException("Unsupported Test type: " + test.getClass().getName());
96+
});
97+
}
98+
99+
private static void runTestCase(TestCase testCase) {
100+
TestResult testResult = new TestResult();
101+
testCase.run(testResult);
102+
assertSuccessfulResults(testResult);
103+
}
104+
105+
private static void assertSuccessfulResults(TestResult testResult) {
106+
if (!testResult.wasSuccessful()) {
107+
Throwable throwable = Stream.concat(stream(testResult.failures()), stream(testResult.errors()))
108+
.map(TestFailure::thrownException)
109+
.findFirst()
110+
.get();
111+
112+
if (throwable instanceof Error error) {
113+
throw error;
114+
}
115+
if (throwable instanceof RuntimeException runtimeException) {
116+
throw runtimeException;
117+
}
118+
throw new AssertionError(throwable);
119+
}
120+
}
61121

62-
return Tck.testsFor(car, false, true);
122+
private static <T> Stream<T> stream(Enumeration<T> enumeration) {
123+
Spliterator<T> spliterator = Spliterators.spliteratorUnknownSize(
124+
enumeration.asIterator(), Spliterator.ORDERED);
125+
return StreamSupport.stream(spliterator, false);
63126
}
64127

65128
}

0 commit comments

Comments
 (0)