Skip to content

Commit 49e5c84

Browse files
committed
Migrate remaining JUnit 4 tests to JUnit Jupiter where feasible
In Spring Framework 5.2, we migrated most of the test suite from JUnit 4 to JUnit Jupiter; however, prior to this commit, several tests in the spring-test module were still based on JUnit 4 unnecessarily. Since we are now planning to deprecate our JUnit 4 support in 7.0, this commit migrates our remaining JUnit 4 based tests to JUnit Jupiter whenever feasible. In the process, test classes that previously resided under the "junit4" package have been moved to new packages directly under the "org.springframework.text.context" package, and several classes have been renamed for greater clarity of purpose. Consequently, the only remaining tests based on JUnit 4 are those tests that are required to run with JUnit 4 in order to test our JUnit 4 support. This commit also greatly simplifies exclusions for Checkstyle rules pertaining to JUnit usage. See gh-23451 See gh-34794 Closes gh-34813
1 parent 56eb135 commit 49e5c84

File tree

136 files changed

+1208
-1461
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+1208
-1461
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,30 +14,26 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.test.context.junit4.spr6128;
17+
package org.springframework.test.context;
1818

19-
import org.junit.Test;
20-
import org.junit.runner.RunWith;
19+
import org.junit.jupiter.api.Test;
2120

2221
import org.springframework.beans.factory.annotation.Autowired;
2322
import org.springframework.beans.factory.annotation.Qualifier;
24-
import org.springframework.test.context.ContextConfiguration;
25-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
23+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
2624

2725
import static org.assertj.core.api.Assertions.assertThat;
2826

2927
/**
30-
* Integration tests to verify claims made in <a
31-
* href="https://jira.springframework.org/browse/SPR-6128"
32-
* target="_blank">SPR-6128</a>.
28+
* Integration tests to verify claims made in
29+
* <a href="https://github.com/spring-projects/spring-framework/issues/10796">gh-10796</a>.
3330
*
3431
* @author Sam Brannen
3532
* @author Chris Beams
3633
* @since 3.0
3734
*/
38-
@ContextConfiguration
39-
@RunWith(SpringJUnit4ClassRunner.class)
40-
public class AutowiredQualifierTests {
35+
@SpringJUnitConfig
36+
class AutowiredQualifierTests {
4137

4238
@Autowired
4339
private String foo;
@@ -48,7 +44,7 @@ public class AutowiredQualifierTests {
4844

4945

5046
@Test
51-
public void test() {
47+
void test() {
5248
assertThat(foo).isEqualTo("normal");
5349
assertThat(customFoo).isEqualTo("custom");
5450
}

spring-test/src/test/java/org/springframework/test/context/ContextHierarchyDirtiesContextTests.java

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,9 +20,8 @@
2020
import org.junit.jupiter.api.MethodOrderer;
2121
import org.junit.jupiter.api.Test;
2222
import org.junit.jupiter.api.TestMethodOrder;
23-
import org.junit.runner.JUnitCore;
24-
import org.junit.runner.Result;
25-
import org.junit.runner.RunWith;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.junit.platform.testkit.engine.EngineTestKit;
2625

2726
import org.springframework.beans.BeansException;
2827
import org.springframework.context.ApplicationContext;
@@ -32,9 +31,10 @@
3231
import org.springframework.context.annotation.Configuration;
3332
import org.springframework.test.annotation.DirtiesContext;
3433
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
35-
import org.springframework.test.context.junit4.SpringRunner;
34+
import org.springframework.test.context.junit.jupiter.SpringExtension;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
3838

3939
/**
4040
* Integration tests that verify proper behavior of {@link DirtiesContext @DirtiesContext}
@@ -87,9 +87,11 @@ void methodLevelDirtiesContextWithExhaustiveHierarchyMode() {
8787
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
8888
boolean isBarContextActive, boolean isBazContextActive) {
8989

90-
JUnitCore jUnitCore = new JUnitCore();
91-
Result result = jUnitCore.run(testClass);
92-
assertThat(result.wasSuccessful()).as("all tests passed").isTrue();
90+
EngineTestKit.engine("junit-jupiter")
91+
.selectors(selectClass(testClass))
92+
.execute()
93+
.testEvents()
94+
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0));
9395

9496
assertThat(ContextHierarchyDirtiesContextTests.context).isNotNull();
9597

@@ -111,7 +113,7 @@ private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass,
111113

112114
// -------------------------------------------------------------------------
113115

114-
@RunWith(SpringRunner.class)
116+
@ExtendWith(SpringExtension.class)
115117
@ContextHierarchy(@ContextConfiguration(name = "foo"))
116118
abstract static class FooTestCase implements ApplicationContextAware {
117119

@@ -170,10 +172,10 @@ String bean() {
170172
* context.
171173
*/
172174
@DirtiesContext
173-
public static class ClassLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
175+
static class ClassLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
174176

175-
@org.junit.Test
176-
public void test() {
177+
@Test
178+
void test() {
177179
}
178180
}
179181

@@ -184,10 +186,10 @@ public void test() {
184186
* beginning from the current context hierarchy and down through all subhierarchies.
185187
*/
186188
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
187-
public static class ClassLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
189+
static class ClassLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
188190

189-
@org.junit.Test
190-
public void test() {
191+
@Test
192+
void test() {
191193
}
192194
}
193195

@@ -199,11 +201,11 @@ public void test() {
199201
* parent context, and then back down through all subhierarchies of the parent
200202
* context.
201203
*/
202-
public static class MethodLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
204+
static class MethodLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
203205

204-
@org.junit.Test
206+
@Test
205207
@DirtiesContext
206-
public void test() {
208+
void test() {
207209
}
208210
}
209211

@@ -213,11 +215,11 @@ public void test() {
213215
* <p>After running this test class, the context cache should be cleared
214216
* beginning from the current context hierarchy and down through all subhierarchies.
215217
*/
216-
public static class MethodLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
218+
static class MethodLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
217219

218-
@org.junit.Test
220+
@Test
219221
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
220-
public void test() {
222+
void test() {
221223
}
222224
}
223225

spring-test/src/test/java/org/springframework/test/context/SpringTestContextFrameworkTestSuite.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.test.context;
1818

19+
import org.junit.jupiter.api.ClassOrderer;
20+
import org.junit.platform.suite.api.ConfigurationParameter;
1921
import org.junit.platform.suite.api.ExcludeTags;
2022
import org.junit.platform.suite.api.IncludeClassNamePatterns;
2123
import org.junit.platform.suite.api.SelectPackages;
@@ -44,5 +46,9 @@
4446
@SelectPackages("org.springframework.test.context")
4547
@IncludeClassNamePatterns(".*Tests?$")
4648
@ExcludeTags("failing-test-case")
49+
@ConfigurationParameter(
50+
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
51+
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
52+
)
4753
class SpringTestContextFrameworkTestSuite {
4854
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2002-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.annotation;
18+
19+
import org.junit.jupiter.api.ClassOrderer;
20+
import org.junit.platform.suite.api.ConfigurationParameter;
21+
import org.junit.platform.suite.api.IncludeClassNamePatterns;
22+
import org.junit.platform.suite.api.IncludeEngines;
23+
import org.junit.platform.suite.api.SelectPackages;
24+
import org.junit.platform.suite.api.Suite;
25+
26+
/**
27+
* JUnit Platform based test suite annotation-driven <em>configuration class</em>
28+
* support in the Spring TestContext Framework.
29+
*
30+
* <p><strong>This suite is only intended to be used manually within an IDE.</strong>
31+
*
32+
* <h3>Logging Configuration</h3>
33+
*
34+
* <p>In order for our log4j2 configuration to be used in an IDE, you must
35+
* set the following system property before running any tests &mdash; for
36+
* example, in <em>Run Configurations</em> in Eclipse.
37+
*
38+
* <pre style="code">
39+
* -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
40+
* </pre>
41+
*
42+
* @author Sam Brannen
43+
* @since 3.1
44+
*/
45+
@Suite
46+
@IncludeEngines("junit-jupiter")
47+
@SelectPackages("org.springframework.test.context.annotation")
48+
@IncludeClassNamePatterns(".*Tests$")
49+
@ConfigurationParameter(
50+
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
51+
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
52+
)
53+
public class AnnotationConfigTestSuite {
54+
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.test.context.junit4.annotation;
17+
package org.springframework.test.context.annotation;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.beans.testfixture.beans.Employee;
2222
import org.springframework.context.annotation.Bean;
@@ -36,13 +36,21 @@
3636
* @since 3.1
3737
*/
3838
@ContextConfiguration
39-
public class BeanOverridingDefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
39+
class BeanOverridingDefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
4040

41-
@Configuration
41+
@Test
42+
@Override
43+
void verifyEmployeeSetFromBaseContextConfig() {
44+
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
45+
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
46+
}
47+
48+
49+
@Configuration(proxyBeanMethods = false)
4250
static class ContextConfiguration {
4351

4452
@Bean
45-
public Employee employee() {
53+
Employee employee() {
4654
Employee employee = new Employee();
4755
employee.setName("Yoda");
4856
employee.setAge(900);
@@ -51,12 +59,4 @@ public Employee employee() {
5159
}
5260
}
5361

54-
55-
@Test
56-
@Override
57-
public void verifyEmployeeSetFromBaseContextConfig() {
58-
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
59-
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
60-
}
61-
6262
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.test.context.junit4.annotation;
17+
package org.springframework.test.context.annotation;
1818

19-
import org.junit.Test;
19+
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.test.context.ContextConfiguration;
2222

@@ -33,11 +33,11 @@
3333
* @since 3.1
3434
*/
3535
@ContextConfiguration(classes = BeanOverridingDefaultConfigClassesInheritedTests.ContextConfiguration.class)
36-
public class BeanOverridingExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
36+
class BeanOverridingExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
3737

3838
@Test
3939
@Override
40-
public void verifyEmployeeSetFromBaseContextConfig() {
40+
void verifyEmployeeSetFromBaseContextConfig() {
4141
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
4242
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
4343
}

0 commit comments

Comments
 (0)