Skip to content

Commit 176b0b0

Browse files
committed
Migrate remaining JUnit 4 tests to JUnit Jupiter where feasible
In 49e5c84 I unfortunately overlooked several JUnit 4 based tests in the `junit4` package that should be migrated to JUnit Jupiter. This commit address those remaining test classes. See gh-23451 See gh-34794 Closes gh-34813
1 parent c48ff35 commit 176b0b0

File tree

59 files changed

+534
-617
lines changed

Some content is hidden

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

59 files changed

+534
-617
lines changed

spring-test/src/test/java/org/springframework/test/context/aot/AotIntegrationTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ void endToEndTestsForBeanOverrides() {
167167
void endToEndTestsForSelectedTestClasses() {
168168
List<Class<?>> testClasses = List.of(
169169
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
170-
org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests.class,
171170
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
172171
);
173172

spring-test/src/test/java/org/springframework/test/context/cache/SpringExtensionContextCacheTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
* @see ContextCacheTests
4848
* @see LruContextCacheTests
4949
*/
50-
@SpringJUnitConfig(locations = "../junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
50+
@SpringJUnitConfig(locations = "../config/CoreContextConfigurationAppCtxTests-context.xml")
5151
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
5252
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
5353
class SpringExtensionContextCacheTests {
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.springframework.test.context.ContextConfiguration;
2020

2121
/**
22-
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
22+
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
2323
* we can specify an explicit, <em>absolute path</em> location for our
2424
* application context.
2525
*
2626
* @author Sam Brannen
2727
* @since 2.5
28-
* @see SpringJUnit4ClassRunnerAppCtxTests
29-
* @see ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests
30-
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
28+
* @see CoreContextConfigurationAppCtxTests
29+
* @see ClassPathResourceContextConfigurationAppCtxTests
30+
* @see RelativePathContextConfigurationAppCtxTests
3131
*/
32-
@ContextConfiguration(locations = { SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
33-
public class AbsolutePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
32+
@ContextConfiguration(locations = CoreContextConfigurationAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH, inheritLocations = false)
33+
class AbsolutePathContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
3434
/* all tests are in the parent class. */
3535
}
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.springframework.test.context.ContextConfiguration;
20-
import org.springframework.test.context.annotation.PojoAndStringConfig;
2120

2221
/**
2322
* Integration tests that verify support for configuration classes in
2423
* the Spring TestContext Framework.
2524
*
26-
* <p>Furthermore, by extending {@code SpringJUnit4ClassRunnerAppCtxTests},
25+
* <p>Furthermore, by extending {@link CoreContextConfigurationAppCtxTests},
2726
* this class also verifies support for several basic features of the
2827
* Spring TestContext Framework. See JavaDoc in
29-
* {@link SpringJUnit4ClassRunnerAppCtxTests} for details.
28+
* {@link CoreContextConfigurationAppCtxTests} for details.
3029
*
3130
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
3231
*
3332
* @author Sam Brannen
3433
* @since 3.1
3534
*/
3635
@ContextConfiguration(classes = PojoAndStringConfig.class, inheritLocations = false)
37-
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
36+
class AnnotationConfigContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
3837
/* all tests are in the parent class. */
3938
}

spring-test/src/test/java/org/springframework/test/context/AutowiredQualifierTests.java renamed to spring-test/src/test/java/org/springframework/test/context/config/AutowiredQualifierTests.java

+25-5
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.test.context;
17+
package org.springframework.test.context.config;
1818

1919
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
2021

2122
import org.springframework.beans.factory.annotation.Autowired;
2223
import org.springframework.beans.factory.annotation.Qualifier;
23-
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.junit.jupiter.SpringExtension;
2428

2529
import static org.assertj.core.api.Assertions.assertThat;
2630

@@ -32,15 +36,16 @@
3236
* @author Chris Beams
3337
* @since 3.0
3438
*/
35-
@SpringJUnitConfig
39+
@ExtendWith(SpringExtension.class)
40+
@ContextConfiguration
3641
class AutowiredQualifierTests {
3742

3843
@Autowired
39-
private String foo;
44+
String foo;
4045

4146
@Autowired
4247
@Qualifier("customFoo")
43-
private String customFoo;
48+
String customFoo;
4449

4550

4651
@Test
@@ -49,4 +54,19 @@ void test() {
4954
assertThat(customFoo).isEqualTo("custom");
5055
}
5156

57+
58+
@Configuration(proxyBeanMethods = false)
59+
static class Config {
60+
61+
@Bean
62+
String foo() {
63+
return "normal";
64+
}
65+
66+
@Bean
67+
String customFoo() {
68+
return "custom";
69+
}
70+
}
71+
5272
}
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.junit.jupiter.api.Test;
2020

Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.junit.jupiter.api.Test;
2020

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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,36 +14,35 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.springframework.test.context.ContextConfiguration;
2020
import org.springframework.util.ResourceUtils;
2121

2222
/**
23-
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
23+
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
2424
* we can specify an explicit, <em>classpath</em> location for our application
2525
* context.
2626
*
2727
* @author Sam Brannen
2828
* @since 2.5
29-
* @see SpringJUnit4ClassRunnerAppCtxTests
29+
* @see CoreContextConfigurationAppCtxTests
3030
* @see #CLASSPATH_CONTEXT_RESOURCE_PATH
31-
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
32-
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
31+
* @see AbsolutePathContextConfigurationAppCtxTests
32+
* @see RelativePathContextConfigurationAppCtxTests
3333
*/
34-
@ContextConfiguration(locations = { ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
35-
public class ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
34+
@ContextConfiguration(locations = { ClassPathResourceContextConfigurationAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
35+
class ClassPathResourceContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
3636

3737
/**
3838
* Classpath-based resource path for the application context configuration
39-
* for {@link SpringJUnit4ClassRunnerAppCtxTests}:
40-
* {@code &quot;classpath:/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;}
39+
* for {@link CoreContextConfigurationAppCtxTests}: {@value}
4140
*
42-
* @see SpringJUnit4ClassRunnerAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
41+
* @see CoreContextConfigurationAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
4342
* @see ResourceUtils#CLASSPATH_URL_PREFIX
4443
*/
4544
public static final String CLASSPATH_CONTEXT_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX +
46-
SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH;
45+
CoreContextConfigurationAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH;
4746

4847
/* all tests are in the parent class. */
4948

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import org.junit.jupiter.api.ClassOrderer;
2020
import org.junit.platform.suite.api.ConfigurationParameter;
@@ -44,11 +44,11 @@
4444
*/
4545
@Suite
4646
@IncludeEngines("junit-jupiter")
47-
@SelectPackages("org.springframework.test.context.annotation")
47+
@SelectPackages("org.springframework.test.context.config")
4848
@IncludeClassNamePatterns(".*Tests$")
4949
@ConfigurationParameter(
5050
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
5151
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
5252
)
53-
public class AnnotationConfigTestSuite {
53+
public class ContextConfigTestSuite {
5454
}
+29-26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 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,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

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

1919
import jakarta.annotation.Resource;
2020
import jakarta.inject.Inject;
2121
import jakarta.inject.Named;
22-
import org.junit.Test;
23-
import org.junit.runner.RunWith;
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
2424

2525
import org.springframework.beans.factory.BeanNameAware;
2626
import org.springframework.beans.factory.InitializingBean;
@@ -33,18 +33,18 @@
3333
import org.springframework.context.ApplicationContextAware;
3434
import org.springframework.test.context.ContextConfiguration;
3535
import org.springframework.test.context.TestExecutionListeners;
36+
import org.springframework.test.context.junit.jupiter.SpringExtension;
3637
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
3738
import org.springframework.test.context.support.GenericXmlContextLoader;
3839

3940
import static org.assertj.core.api.Assertions.assertThat;
4041

4142
/**
42-
* SpringJUnit4ClassRunnerAppCtxTests serves as a <em>proof of concept</em>
43-
* JUnit 4 based test class, which verifies the expected functionality of
44-
* {@link SpringRunner} in conjunction with the following:
43+
* {@code CoreContextConfigurationAppCtxTests} serves as a <em>core</em> test class, which
44+
* verifies the expected functionality of {@link ContextConfiguration @ContextConfiguration}
45+
* in conjunction with the following:
4546
*
4647
* <ul>
47-
* <li>{@link ContextConfiguration @ContextConfiguration}</li>
4848
* <li>{@link Autowired @Autowired}</li>
4949
* <li>{@link Qualifier @Qualifier}</li>
5050
* <li>{@link Resource @Resource}</li>
@@ -67,21 +67,24 @@
6767
*
6868
* @author Sam Brannen
6969
* @since 2.5
70-
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
71-
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
72-
* @see InheritedConfigSpringJUnit4ClassRunnerAppCtxTests
70+
* @see AbsolutePathContextConfigurationAppCtxTests
71+
* @see AnnotationConfigContextConfigurationAppCtxTests
72+
* @see ClassPathResourceContextConfigurationAppCtxTests
73+
* @see InheritedConfigContextConfigurationAppCtxTests
74+
* @see MultipleResourcesContextConfigurationAppCtxTests
75+
* @see RelativePathContextConfigurationAppCtxTests
7376
*/
74-
@RunWith(SpringRunner.class)
77+
@ExtendWith(SpringExtension.class)
7578
@ContextConfiguration
7679
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
77-
public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAware, BeanNameAware, InitializingBean {
80+
class CoreContextConfigurationAppCtxTests implements ApplicationContextAware, BeanNameAware, InitializingBean {
7881

7982
/**
8083
* Default resource path for the application context configuration for
81-
* {@link SpringJUnit4ClassRunnerAppCtxTests}: {@value}
84+
* {@link CoreContextConfigurationAppCtxTests}: {@value}
8285
*/
8386
public static final String DEFAULT_CONTEXT_RESOURCE_PATH =
84-
"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
87+
"/org/springframework/test/context/config/CoreContextConfigurationAppCtxTests-context.xml";
8588

8689

8790
private Employee employee;
@@ -136,12 +139,12 @@ protected void setBar(String bar) {
136139
}
137140

138141
@Autowired
139-
public void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
142+
void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
140143
this.literalParameterValue = literalParameterValue;
141144
}
142145

143146
@Autowired
144-
public void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
147+
void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
145148
this.spelParameterValue = spelParameterValue;
146149
}
147150

@@ -162,23 +165,23 @@ public void afterPropertiesSet() {
162165

163166

164167
@Test
165-
public void verifyBeanNameSet() {
168+
void verifyBeanNameSet() {
166169
assertThat(this.beanName).as("The bean name of this test instance should have been set due to BeanNameAware semantics.")
167170
.startsWith(getClass().getName());
168171
}
169172

170173
@Test
171-
public void verifyApplicationContextSet() {
174+
void verifyApplicationContextSet() {
172175
assertThat(this.applicationContext).as("The application context should have been set due to ApplicationContextAware semantics.").isNotNull();
173176
}
174177

175178
@Test
176-
public void verifyBeanInitialized() {
179+
void verifyBeanInitialized() {
177180
assertThat(this.beanInitialized).as("This test bean should have been initialized due to InitializingBean semantics.").isTrue();
178181
}
179182

180183
@Test
181-
public void verifyAnnotationAutowiredAndInjectedFields() {
184+
void verifyAnnotationAutowiredAndInjectedFields() {
182185
assertThat(this.nonrequiredLong).as("The nonrequiredLong field should NOT have been autowired.").isNull();
183186
assertThat(this.quux).as("The quux field should have been autowired via @Autowired and @Qualifier.").isEqualTo("Quux");
184187
assertThat(this.namedQuux).as("The namedFoo field should have been injected via @Inject and @Named.").isEqualTo("Quux");
@@ -192,34 +195,34 @@ public void verifyAnnotationAutowiredAndInjectedFields() {
192195
}
193196

194197
@Test
195-
public void verifyAnnotationAutowiredMethods() {
198+
void verifyAnnotationAutowiredMethods() {
196199
assertThat(this.employee).as("The employee setter method should have been autowired.").isNotNull();
197200
assertThat(this.employee.getName()).isEqualTo("John Smith");
198201
}
199202

200203
@Test
201-
public void verifyAutowiredAtValueFields() {
204+
void verifyAutowiredAtValueFields() {
202205
assertThat(this.literalFieldValue).as("Literal @Value field should have been autowired").isNotNull();
203206
assertThat(this.spelFieldValue).as("SpEL @Value field should have been autowired.").isNotNull();
204207
assertThat(this.literalFieldValue).isEqualTo("enigma");
205208
assertThat(this.spelFieldValue).isTrue();
206209
}
207210

208211
@Test
209-
public void verifyAutowiredAtValueMethods() {
212+
void verifyAutowiredAtValueMethods() {
210213
assertThat(this.literalParameterValue).as("Literal @Value method parameter should have been autowired.").isNotNull();
211214
assertThat(this.spelParameterValue).as("SpEL @Value method parameter should have been autowired.").isNotNull();
212215
assertThat(this.literalParameterValue).isEqualTo("enigma");
213216
assertThat(this.spelParameterValue).isTrue();
214217
}
215218

216219
@Test
217-
public void verifyResourceAnnotationInjectedFields() {
220+
void verifyResourceAnnotationInjectedFields() {
218221
assertThat(this.foo).as("The foo field should have been injected via @Resource.").isEqualTo("Foo");
219222
}
220223

221224
@Test
222-
public void verifyResourceAnnotationInjectedMethods() {
225+
void verifyResourceAnnotationInjectedMethods() {
223226
assertThat(this.bar).as("The bar method should have been wired via @Resource.").isEqualTo("Bar");
224227
}
225228

0 commit comments

Comments
 (0)