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
This commit is contained in:
Sam Brannen
2025-04-24 16:13:10 +02:00
parent 56eb135608
commit 49e5c84928
136 changed files with 1211 additions and 1464 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,30 +14,26 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr6128;
package org.springframework.test.context;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests to verify claims made in <a
* href="https://jira.springframework.org/browse/SPR-6128"
* target="_blank">SPR-6128</a>.
* Integration tests to verify claims made in
* <a href="https://github.com/spring-projects/spring-framework/issues/10796">gh-10796</a>.
*
* @author Sam Brannen
* @author Chris Beams
* @since 3.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class AutowiredQualifierTests {
@SpringJUnitConfig
class AutowiredQualifierTests {
@Autowired
private String foo;
@@ -48,7 +44,7 @@ public class AutowiredQualifierTests {
@Test
public void test() {
void test() {
assertThat(foo).isEqualTo("normal");
assertThat(customFoo).isEqualTo("custom");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,8 @@ import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
@@ -32,9 +31,10 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
/**
* Integration tests that verify proper behavior of {@link DirtiesContext @DirtiesContext}
@@ -87,9 +87,11 @@ class ContextHierarchyDirtiesContextTests {
private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass, boolean isFooContextActive,
boolean isBarContextActive, boolean isBazContextActive) {
JUnitCore jUnitCore = new JUnitCore();
Result result = jUnitCore.run(testClass);
assertThat(result.wasSuccessful()).as("all tests passed").isTrue();
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(testClass))
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(1).succeeded(1).failed(0));
assertThat(ContextHierarchyDirtiesContextTests.context).isNotNull();
@@ -111,7 +113,7 @@ class ContextHierarchyDirtiesContextTests {
// -------------------------------------------------------------------------
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ContextHierarchy(@ContextConfiguration(name = "foo"))
abstract static class FooTestCase implements ApplicationContextAware {
@@ -170,10 +172,10 @@ class ContextHierarchyDirtiesContextTests {
* context.
*/
@DirtiesContext
public static class ClassLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
static class ClassLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
@org.junit.Test
public void test() {
@Test
void test() {
}
}
@@ -184,10 +186,10 @@ class ContextHierarchyDirtiesContextTests {
* beginning from the current context hierarchy and down through all subhierarchies.
*/
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
public static class ClassLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
static class ClassLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
@org.junit.Test
public void test() {
@Test
void test() {
}
}
@@ -199,11 +201,11 @@ class ContextHierarchyDirtiesContextTests {
* parent context, and then back down through all subhierarchies of the parent
* context.
*/
public static class MethodLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
static class MethodLevelDirtiesContextWithExhaustiveModeTestCase extends BazTestCase {
@org.junit.Test
@Test
@DirtiesContext
public void test() {
void test() {
}
}
@@ -213,11 +215,11 @@ class ContextHierarchyDirtiesContextTests {
* <p>After running this test class, the context cache should be cleared
* beginning from the current context hierarchy and down through all subhierarchies.
*/
public static class MethodLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
static class MethodLevelDirtiesContextWithCurrentLevelModeTestCase extends BazTestCase {
@org.junit.Test
@Test
@DirtiesContext(hierarchyMode = HierarchyMode.CURRENT_LEVEL)
public void test() {
void test() {
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.test.context;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.ExcludeTags;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.SelectPackages;
@@ -44,5 +46,9 @@ import org.junit.platform.suite.api.Suite;
@SelectPackages("org.springframework.test.context")
@IncludeClassNamePatterns(".*Tests?$")
@ExcludeTags("failing-test-case")
@ConfigurationParameter(
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
)
class SpringTestContextFrameworkTestSuite {
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.annotation;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
/**
* JUnit Platform based test suite annotation-driven <em>configuration class</em>
* support in the Spring TestContext Framework.
*
* <p><strong>This suite is only intended to be used manually within an IDE.</strong>
*
* <h3>Logging Configuration</h3>
*
* <p>In order for our log4j2 configuration to be used in an IDE, you must
* set the following system property before running any tests &mdash; for
* example, in <em>Run Configurations</em> in Eclipse.
*
* <pre style="code">
* -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
* </pre>
*
* @author Sam Brannen
* @since 3.1
*/
@Suite
@IncludeEngines("junit-jupiter")
@SelectPackages("org.springframework.test.context.annotation")
@IncludeClassNamePatterns(".*Tests$")
@ConfigurationParameter(
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
)
public class AnnotationConfigTestSuite {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
@@ -36,13 +36,21 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration
public class BeanOverridingDefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
class BeanOverridingDefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
@Configuration
@Test
@Override
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
@Configuration(proxyBeanMethods = false)
static class ContextConfiguration {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("Yoda");
employee.setAge(900);
@@ -51,12 +59,4 @@ public class BeanOverridingDefaultConfigClassesInheritedTests extends DefaultCon
}
}
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ContextConfiguration;
@@ -33,11 +33,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration(classes = BeanOverridingDefaultConfigClassesInheritedTests.ContextConfiguration.class)
public class BeanOverridingExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
class BeanOverridingExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,15 +37,25 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
* @see DefaultLoaderDefaultConfigClassesBaseTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class DefaultConfigClassesBaseTests {
@SpringJUnitConfig(loader = AnnotationConfigContextLoader.class)
class DefaultConfigClassesBaseTests {
@Configuration
@Autowired
Employee employee;
@Test
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee field should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
@Configuration(proxyBeanMethods = false)
static class ContextConfiguration {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
@@ -56,15 +64,4 @@ public class DefaultConfigClassesBaseTests {
}
}
@Autowired
protected Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee field should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
@@ -37,26 +37,26 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration
public class DefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
@Configuration
static class ContextConfiguration {
@Bean
public Pet pet() {
return new Pet("Fido");
}
}
class DefaultConfigClassesInheritedTests extends DefaultConfigClassesBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}
@Configuration(proxyBeanMethods = false)
static class ContextConfiguration {
@Bean
Pet pet() {
return new Pet("Fido");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
@@ -35,14 +35,22 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration
public class DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests extends
class DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests extends
DefaultLoaderDefaultConfigClassesBaseTests {
@Configuration
@Test
@Override
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("Yoda");
employee.setAge(900);
@@ -51,12 +59,4 @@ public class DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests exten
}
}
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.DelegatingSmartContextLoader;
@@ -32,12 +32,12 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration(classes = DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.Config.class)
public class DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests extends
class DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests extends
DefaultLoaderExplicitConfigClassesBaseTests {
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DelegatingSmartContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,15 +36,25 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
* @see DefaultConfigClassesBaseTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DefaultLoaderDefaultConfigClassesBaseTests {
@SpringJUnitConfig
class DefaultLoaderDefaultConfigClassesBaseTests {
@Configuration
@Autowired
Employee employee;
@Test
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee field should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
@@ -55,15 +63,4 @@ public class DefaultLoaderDefaultConfigClassesBaseTests {
}
}
@Autowired
protected Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee field should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
@@ -36,26 +36,26 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ContextConfiguration
public class DefaultLoaderDefaultConfigClassesInheritedTests extends DefaultLoaderDefaultConfigClassesBaseTests {
@Configuration
static class Config {
@Bean
public Pet pet() {
return new Pet("Fido");
}
}
class DefaultLoaderDefaultConfigClassesInheritedTests extends DefaultLoaderDefaultConfigClassesBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
Pet pet() {
return new Pet("Fido");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,15 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DelegatingSmartContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,16 +33,15 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DefaultLoaderDefaultConfigClassesBaseTests.Config.class)
public class DefaultLoaderExplicitConfigClassesBaseTests {
@SpringJUnitConfig(DefaultLoaderDefaultConfigClassesBaseTests.Config.class)
class DefaultLoaderExplicitConfigClassesBaseTests {
@Autowired
protected Employee employee;
Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,15 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DelegatingSmartContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,16 +33,15 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DefaultLoaderDefaultConfigClassesInheritedTests.Config.class)
public class DefaultLoaderExplicitConfigClassesInheritedTests extends DefaultLoaderExplicitConfigClassesBaseTests {
@SpringJUnitConfig(DefaultLoaderDefaultConfigClassesInheritedTests.Config.class)
class DefaultLoaderExplicitConfigClassesInheritedTests extends DefaultLoaderExplicitConfigClassesBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,15 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,16 +34,15 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = DefaultConfigClassesBaseTests.ContextConfiguration.class)
public class ExplicitConfigClassesBaseTests {
@SpringJUnitConfig(loader = AnnotationConfigContextLoader.class, classes = DefaultConfigClassesBaseTests.ContextConfiguration.class)
class ExplicitConfigClassesBaseTests {
@Autowired
protected Employee employee;
Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,15 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,16 +35,15 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = DefaultConfigClassesInheritedTests.ContextConfiguration.class)
public class ExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
@SpringJUnitConfig(loader = AnnotationConfigContextLoader.class, classes = DefaultConfigClassesInheritedTests.ContextConfiguration.class)
class ExplicitConfigClassesInheritedTests extends ExplicitConfigClassesBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.annotation;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.Pet;
@@ -32,7 +32,7 @@ import org.springframework.context.annotation.Configuration;
* @author Sam Brannen
* @since 3.1
*/
@Configuration
@Configuration(proxyBeanMethods = false)
public class PojoAndStringConfig {
@Bean

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,16 +31,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig
public class ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfigTests {
class ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfigTests {
@Autowired
private String foo;
@Test
public void foo() {
void foo() {
assertThat(foo).isEqualTo("Resolver Foo");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,17 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.ActiveProfilesResolver;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,18 +35,19 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfig(classes = LocalDevConfig.class, resolver = DevResolver.class)
public class ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfigWithOverridesTests {
class ConfigClassesAndProfileResolverWithCustomDefaultsMetaConfigWithOverridesTests {
@Autowired
private String foo;
@Test
public void foo() {
void foo() {
assertThat(foo).isEqualTo("Local Dev Foo");
}
}
@Configuration

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,37 +35,38 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ConfigClassesAndProfilesMetaConfig(profiles = "dev")
public class ConfigClassesAndProfilesMetaConfigTests {
class ConfigClassesAndProfilesMetaConfigTests {
@Configuration
@Autowired
String foo;
@Test
void foo() {
assertThat(foo).isEqualTo("Local Dev Foo");
}
@Configuration(proxyBeanMethods = false)
@Profile("dev")
static class DevConfig {
@Bean
public String foo() {
String foo() {
return "Local Dev Foo";
}
}
@Configuration
@Configuration(proxyBeanMethods = false)
@Profile("prod")
static class ProductionConfig {
@Bean
public String foo() {
String foo() {
return "Local Production Foo";
}
}
@Autowired
private String foo;
@Test
public void foo() {
assertThat(foo).isEqualTo("Local Dev Foo");
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,16 +31,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ConfigClassesAndProfilesWithCustomDefaultsMetaConfig
public class ConfigClassesAndProfilesWithCustomDefaultsMetaConfigTests {
class ConfigClassesAndProfilesWithCustomDefaultsMetaConfigTests {
@Autowired
private String foo;
String foo;
@Test
public void foo() {
void foo() {
assertThat(foo).isEqualTo("Dev Foo");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,17 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.annotation.PojoAndStringConfig;
import org.springframework.test.context.junit4.annotation.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
import org.springframework.test.context.annotation.PojoAndStringConfig;
import org.springframework.test.context.annotation.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -35,35 +35,35 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ConfigClassesAndProfilesWithCustomDefaultsMetaConfig(
classes = { PojoAndStringConfig.class, ProductionConfig.class }, profiles = "prod")
public class ConfigClassesAndProfilesWithCustomDefaultsMetaConfigWithOverridesTests {
class ConfigClassesAndProfilesWithCustomDefaultsMetaConfigWithOverridesTests {
@Autowired
private String foo;
String foo;
@Autowired
private Pet pet;
Pet pet;
@Autowired
protected Employee employee;
Employee employee;
@Test
public void verifyEmployee() {
void verifyEmployee() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
@Test
public void verifyPet() {
void verifyPet() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}
@Test
public void verifyFoo() {
void verifyFoo() {
assertThat(this.foo).isEqualTo("Production Foo");
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation.meta;
package org.springframework.test.context.annotation.meta;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,16 +32,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.0.3
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@MetaMetaConfig
public class MetaMetaConfigDefaultsTests {
class MetaMetaConfigDefaultsTests {
@Autowired
private String foo;
String foo;
@Test
public void foo() {
void foo() {
assertThat(foo).isEqualTo("Production Foo");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@ import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.TrackingTestNGTestListener;
@@ -162,12 +163,18 @@ class ClassLevelDirtiesContextTestNGTests {
// -------------------------------------------------------------------
@TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class }, inheritListeners = false)
@ContextConfiguration
// Ensure that we do not include the EventPublishingTestExecutionListener
// since it will access the ApplicationContext for each method in the
// TestExecutionListener API, thus distorting our cache hit/miss results.
@TestExecutionListeners({
DirtiesContextBeforeModesTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class
})
abstract static class BaseTestCase extends AbstractTestNGSpringContextTests {
@Configuration
@Configuration(proxyBeanMethods = false)
static class Config {
/* no beans */
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
@@ -31,15 +32,14 @@ import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.springframework.test.context.cache.ContextCacheTestUtils.assertContextCacheStatistics;
import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache;
import static org.springframework.test.context.junit4.JUnitTestingUtils.runTestsAndAssertCounters;
/**
* JUnit based integration test which verifies correct {@linkplain ContextCache
@@ -131,15 +131,24 @@ class ClassLevelDirtiesContextTests {
0, cacheHits.incrementAndGet(), cacheMisses.get());
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) throws Exception {
runTestsAndAssertCounters(testClass, expectedTestCount, 0, expectedTestCount, 0, 0);
}
private void assertBehaviorForCleanTestCase() throws Exception {
runTestClassAndAssertStats(CleanTestCase.class, 1);
assertContextCacheStatistics("after clean test class", 1, cacheHits.get(), cacheMisses.incrementAndGet());
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) throws Exception {
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(testClass))
.execute()
.testEvents()
.assertStatistics(stats -> stats
.started(expectedTestCount)
.finished(expectedTestCount)
.succeeded(expectedTestCount)
.failed(0)
.aborted(0));
}
@AfterAll
static void verifyFinalCacheState() {
assertContextCacheStatistics("AfterClass", 0, cacheHits.get(), cacheMisses.get());
@@ -148,7 +157,7 @@ class ClassLevelDirtiesContextTests {
// -------------------------------------------------------------------
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration
// Ensure that we do not include the EventPublishingTestExecutionListener
// since it will access the ApplicationContext for each method in the
@@ -160,7 +169,7 @@ class ClassLevelDirtiesContextTests {
})
abstract static class BaseTestCase {
@Configuration
@Configuration(proxyBeanMethods = false)
static class Config {
/* no beans */
}
@@ -175,75 +184,75 @@ class ClassLevelDirtiesContextTests {
}
}
public static final class CleanTestCase extends BaseTestCase {
static final class CleanTestCase extends BaseTestCase {
@org.junit.Test
public void verifyContextWasAutowired() {
@Test
void verifyContextWasAutowired() {
assertApplicationContextWasAutowired();
}
}
@DirtiesContext
public static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase {
static class ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends BaseTestCase {
@org.junit.Test
public void verifyContextWasAutowired() {
@Test
void verifyContextWasAutowired() {
assertApplicationContextWasAutowired();
}
}
public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends
static class InheritedClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase extends
ClassLevelDirtiesContextWithCleanMethodsAndDefaultModeTestCase {
}
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public static class ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends BaseTestCase {
static class ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends BaseTestCase {
@org.junit.Test
public void verifyContextWasAutowired() {
@Test
void verifyContextWasAutowired() {
assertApplicationContextWasAutowired();
}
}
public static class InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends
static class InheritedClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase extends
ClassLevelDirtiesContextWithCleanMethodsAndAfterClassModeTestCase {
}
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public static class ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends BaseTestCase {
static class ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends BaseTestCase {
@org.junit.Test
public void verifyContextWasAutowired1() {
@Test
void verifyContextWasAutowired1() {
assertApplicationContextWasAutowired();
}
@org.junit.Test
public void verifyContextWasAutowired2() {
@Test
void verifyContextWasAutowired2() {
assertApplicationContextWasAutowired();
}
@org.junit.Test
public void verifyContextWasAutowired3() {
@Test
void verifyContextWasAutowired3() {
assertApplicationContextWasAutowired();
}
}
public static class InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends
static class InheritedClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase extends
ClassLevelDirtiesContextWithAfterEachTestMethodModeTestCase {
}
@DirtiesContext
public static class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase {
static class ClassLevelDirtiesContextWithDirtyMethodsTestCase extends BaseTestCase {
@org.junit.Test
@Test
@DirtiesContext
public void dirtyContext() {
void dirtyContext() {
assertApplicationContextWasAutowired();
}
}
public static class InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase extends
static class InheritedClassLevelDirtiesContextWithDirtyMethodsTestCase extends
ClassLevelDirtiesContextWithDirtyMethodsTestCase {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.annotation.DirtiesContext.MethodMode.BEFORE_METHOD;
/**
* Integration test which verifies correct interaction between the
* Integration tests which verify correct interaction between the
* {@link DirtiesContextBeforeModesTestExecutionListener},
* {@link DependencyInjectionTestExecutionListener}, and
* {@link DirtiesContextTestExecutionListener} when

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,10 +37,9 @@ import static org.springframework.test.context.cache.ContextCacheTestUtils.asser
import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache;
/**
* Unit tests which verify correct {@link ContextCache
* application context caching} in conjunction with the
* {@link SpringExtension} and the {@link DirtiesContext
* &#064;DirtiesContext} annotation at the method level.
* JUnit based integration test which verifies correct {@linkplain ContextCache
* application context caching} in conjunction with the {@link SpringExtension} and
* {@link DirtiesContext @DirtiesContext} at the method level.
*
* @author Sam Brannen
* @author Juergen Hoeller

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,21 +21,22 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.springframework.test.context.cache.ContextCacheTestUtils.assertContextCacheStatistics;
import static org.springframework.test.context.cache.ContextCacheTestUtils.resetContextCache;
import static org.springframework.test.context.junit4.JUnitTestingUtils.runTestsAndAssertCounters;
/**
* @author Sam Brannen
@@ -70,11 +71,14 @@ class DirtiesContextInterfaceTests {
}
private void runTestClassAndAssertStats(Class<?> testClass, int expectedTestCount) throws Exception {
runTestsAndAssertCounters(testClass, expectedTestCount, 0, expectedTestCount, 0, 0);
EngineTestKit.engine("junit-jupiter")
.selectors(selectClass(testClass))
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(expectedTestCount).succeeded(expectedTestCount).failed(0));
}
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
// Ensure that we do not include the EventPublishingTestExecutionListener
// since it will access the ApplicationContext for each method in the
// TestExecutionListener API, thus distorting our cache hit/miss results.
@@ -90,13 +94,13 @@ class DirtiesContextInterfaceTests {
ApplicationContext applicationContext;
@org.junit.Test
@Test
public void verifyContextWasAutowired() {
assertThat(this.applicationContext).as("The application context should have been autowired.").isNotNull();
}
@Configuration
@Configuration(proxyBeanMethods = false)
static class Config {
/* no beans */
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.hybrid;
package org.springframework.test.context.hybrid;
import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,17 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.hybrid;
package org.springframework.test.context.hybrid;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.SmartContextLoader;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,37 +37,22 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 4.0.4
* @see HybridContextLoader
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(loader = HybridContextLoader.class)
public class HybridContextLoaderTests {
@Configuration
static class Config {
@Bean
public String fooFromJava() {
return "Java";
}
@Bean
public String enigma() {
return "enigma from Java";
}
}
class HybridContextLoaderTests {
@Autowired
private String fooFromXml;
String fooFromXml;
@Autowired
private String fooFromJava;
String fooFromJava;
@Autowired
private String enigma;
String enigma;
@Test
public void verifyContentsOfHybridApplicationContext() {
void verifyContentsOfHybridApplicationContext() {
assertThat(fooFromXml).isEqualTo("XML");
assertThat(fooFromJava).isEqualTo("Java");
@@ -78,4 +63,19 @@ public class HybridContextLoaderTests {
assertThat(enigma).isEqualTo("enigma from XML");
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
String fooFromJava() {
return "Java";
}
@Bean
String enigma() {
return "enigma from Java";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -33,12 +33,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 2.5
*/
@ContextConfiguration
public class BeanOverridingDefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
class BeanOverridingDefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,16 +14,16 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -33,12 +33,13 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 2.5
*/
@ContextConfiguration("BeanOverridingDefaultLocationsInheritedTests-context.xml")
public class BeanOverridingExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
class BeanOverridingExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
@Test
@Override
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).as("The employee bean should have been overridden.").isEqualTo("Yoda");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,20 +14,19 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -36,17 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 2.5
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class DefaultLocationsBaseTests {
@SpringJUnitConfig
class DefaultLocationsBaseTests {
@Autowired
protected Employee employee;
Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
@@ -25,7 +25,7 @@ import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -35,15 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 2.5
*/
@ContextConfiguration
public class DefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
class DefaultLocationsInheritedTests extends DefaultLocationsBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,20 +14,19 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -36,17 +35,17 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 2.5
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("DefaultLocationsBaseTests-context.xml")
public class ExplicitLocationsBaseTests {
@SpringJUnitConfig(locations = "DefaultLocationsBaseTests-context.xml")
class ExplicitLocationsBaseTests {
@Autowired
protected Employee employee;
Employee employee;
@Test
public void verifyEmployeeSetFromBaseContextConfig() {
void verifyEmployeeSetFromBaseContextConfig() {
assertThat(this.employee).as("The employee should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
package org.springframework.test.context.inheritance;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Pet;
@@ -25,7 +25,7 @@ import org.springframework.test.context.ContextConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test for verifying support for the
* JUnit based integration test for verifying support for the
* {@link ContextConfiguration#inheritLocations() inheritLocations} flag of
* {@link ContextConfiguration @ContextConfiguration} indirectly proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
@@ -35,15 +35,16 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 2.5
*/
@ContextConfiguration("DefaultLocationsInheritedTests-context.xml")
public class ExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
class ExplicitLocationsInheritedTests extends ExplicitLocationsBaseTests {
@Autowired
private Pet pet;
Pet pet;
@Test
public void verifyPetSetFromExtendedContextConfig() {
void verifyPetSetFromExtendedContextConfig() {
assertThat(this.pet).as("The pet should have been autowired.").isNotNull();
assertThat(this.pet.getName()).isEqualTo("Fido");
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.initializers;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.platform.suite.api.ConfigurationParameter;
import org.junit.platform.suite.api.ExcludeTags;
import org.junit.platform.suite.api.IncludeClassNamePatterns;
import org.junit.platform.suite.api.IncludeEngines;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
import org.springframework.context.ApplicationContextInitializer;
/**
* JUnit Platform based test suite for tests that involve
* {@link ApplicationContextInitializer ApplicationContextInitializers} (ACIs)
* in the TestContext framework.
*
* <p><strong>This suite is only intended to be used manually within an IDE.</strong>
*
* <h3>Logging Configuration</h3>
*
* <p>In order for our log4j2 configuration to be used in an IDE, you must
* set the following system property before running any tests &mdash; for
* example, in <em>Run Configurations</em> in Eclipse.
*
* <pre style="code">
* -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
* </pre>
*
* @author Sam Brannen
* @since 3.2
*/
@Suite
@IncludeEngines("junit-jupiter")
@SelectPackages({
"org.springframework.test.context.initializers.annotation",
"org.springframework.test.context.initializers.xml"
})
@IncludeClassNamePatterns(".*Tests$")
@ExcludeTags("failing-test-case")
@ConfigurationParameter(
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
)
public class AciTestSuite {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci;
package org.springframework.test.context.initializers;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
@@ -29,4 +29,5 @@ public class DevProfileInitializer implements ApplicationContextInitializer<Gene
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.getEnvironment().setActiveProfiles("dev");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci;
package org.springframework.test.context.initializers;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.support.GenericApplicationContext;
@@ -29,4 +29,5 @@ public class FooBarAliasInitializer implements ApplicationContextInitializer<Gen
public void initialize(GenericApplicationContext applicationContext) {
applicationContext.registerAlias("foo", "bar");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -23,7 +23,7 @@ import org.springframework.context.annotation.Configuration;
* @author Sam Brannen
* @since 4.3
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class BarConfig {
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -24,7 +24,7 @@ import org.springframework.context.annotation.Profile;
* @author Sam Brannen
* @since 3.2
*/
@Configuration
@Configuration(proxyBeanMethods = false)
@Profile("dev")
class DevProfileConfig {
@@ -32,4 +32,5 @@ class DevProfileConfig {
public String baz() {
return "dev profile config";
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -23,7 +23,7 @@ import org.springframework.context.annotation.Configuration;
* @author Sam Brannen
* @since 4.3
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class FooConfig {
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -23,7 +23,7 @@ import org.springframework.context.annotation.Configuration;
* @author Sam Brannen
* @since 3.2
*/
@Configuration
@Configuration(proxyBeanMethods = false)
class GlobalConfig {
@Bean
@@ -35,4 +35,5 @@ class GlobalConfig {
public String baz() {
return "global config";
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -22,8 +22,8 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
@@ -31,8 +31,8 @@ import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit4.aci.annotation.InitializerConfiguredViaMetaAnnotationTests.ComposedContextConfiguration;
import org.springframework.test.context.initializers.annotation.InitializerConfiguredViaMetaAnnotationTests.ComposedContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -51,7 +51,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ComposedContextConfiguration(BarConfig.class)
public class InitializerConfiguredViaMetaAnnotationTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,18 +14,16 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.aci.annotation.InitializerWithoutConfigFilesOrClassesTests.EntireAppInitializer;
import org.springframework.test.context.initializers.annotation.InitializerWithoutConfigFilesOrClassesTests.EntireAppInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -37,8 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = EntireAppInitializer.class)
@SpringJUnitConfig(initializers = EntireAppInitializer.class)
public class InitializerWithoutConfigFilesOrClassesTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.aci.DevProfileInitializer;
import org.springframework.test.context.initializers.DevProfileInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@ContextConfiguration(initializers = DevProfileInitializer.class)
@SpringJUnitConfig(initializers = DevProfileInitializer.class)
public class MergedInitializersAnnotationConfigTests extends SingleInitializerAnnotationConfigTests {
@Override
@@ -42,4 +42,5 @@ public class MergedInitializersAnnotationConfigTests extends SingleInitializerAn
assertThat(bar).isEqualTo("foo");
assertThat(baz).isEqualTo("dev profile config");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.aci.DevProfileInitializer;
import org.springframework.test.context.junit4.aci.FooBarAliasInitializer;
import org.springframework.test.context.initializers.DevProfileInitializer;
import org.springframework.test.context.initializers.FooBarAliasInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,8 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { GlobalConfig.class, DevProfileConfig.class }, initializers = {
@SpringJUnitConfig(classes = { GlobalConfig.class, DevProfileConfig.class }, initializers = {
FooBarAliasInitializer.class, DevProfileInitializer.class })
public class MultipleInitializersAnnotationConfigTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,10 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
@@ -27,13 +26,12 @@ import org.springframework.context.annotation.Profile;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests.ConfigOne;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests.ConfigTwo;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests.GlobalConfig;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests.OrderedOneInitializer;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests.OrderedTwoInitializer;
import org.springframework.test.context.initializers.annotation.OrderedInitializersAnnotationConfigTests.ConfigOne;
import org.springframework.test.context.initializers.annotation.OrderedInitializersAnnotationConfigTests.ConfigTwo;
import org.springframework.test.context.initializers.annotation.OrderedInitializersAnnotationConfigTests.GlobalConfig;
import org.springframework.test.context.initializers.annotation.OrderedInitializersAnnotationConfigTests.OrderedOneInitializer;
import org.springframework.test.context.initializers.annotation.OrderedInitializersAnnotationConfigTests.OrderedTwoInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -48,10 +46,9 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
// Note: the ordering of the config classes is intentionally: global, two, one.
// Note: the ordering of the initializers is intentionally: two, one.
@ContextConfiguration(classes = { GlobalConfig.class, ConfigTwo.class, ConfigOne.class }, initializers = {
@SpringJUnitConfig(classes = { GlobalConfig.class, ConfigTwo.class, ConfigOne.class }, initializers = {
OrderedTwoInitializer.class, OrderedOneInitializer.class })
public class OrderedInitializersAnnotationConfigTests {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.aci.DevProfileInitializer;
import org.springframework.test.context.initializers.DevProfileInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@ContextConfiguration(initializers = DevProfileInitializer.class, inheritInitializers = false)
@SpringJUnitConfig(initializers = DevProfileInitializer.class, inheritInitializers = false)
public class OverriddenInitializersAnnotationConfigTests extends SingleInitializerAnnotationConfigTests {
@Test
@@ -42,4 +42,5 @@ public class OverriddenInitializersAnnotationConfigTests extends SingleInitializ
assertThat(bar).isNull();
assertThat(baz).isEqualTo("dev profile config");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,10 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@@ -27,8 +26,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -39,8 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 4.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = PropertySourcesInitializerTests.PropertySourceInitializer.class)
@SpringJUnitConfig(initializers = PropertySourcesInitializerTests.PropertySourceInitializer.class)
public class PropertySourcesInitializerTests {
@Configuration

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.annotation;
package org.springframework.test.context.initializers.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.aci.FooBarAliasInitializer;
import org.springframework.test.context.initializers.FooBarAliasInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,8 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { GlobalConfig.class, DevProfileConfig.class }, initializers = FooBarAliasInitializer.class)
@SpringJUnitConfig(classes = { GlobalConfig.class, DevProfileConfig.class }, initializers = FooBarAliasInitializer.class)
public class SingleInitializerAnnotationConfigTests {
@Autowired

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,17 +14,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci.xml;
package org.springframework.test.context.initializers.xml;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.aci.DevProfileInitializer;
import org.springframework.test.context.junit4.aci.FooBarAliasInitializer;
import org.springframework.test.context.initializers.DevProfileInitializer;
import org.springframework.test.context.initializers.FooBarAliasInitializer;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,8 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = { FooBarAliasInitializer.class, DevProfileInitializer.class })
@SpringJUnitConfig(initializers = { FooBarAliasInitializer.class, DevProfileInitializer.class })
public class MultipleInitializersXmlConfigTests {
@Autowired

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.jdbc;
import javax.sql.DataSource;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.testkit.engine.EngineTestKit;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
/**
* Test suite to investigate claims raised in
* <a href="https://github.com/spring-projects/spring-framework/issues/13491">gh-13491</a>.
*
* <h3>Work Around</h3>
* <p>By using a SpEL expression to generate a random {@code database-name}
* for the embedded database (see {@code datasource-config.xml}), we ensure
* that each {@code ApplicationContext} that imports the common configuration
* will create an embedded database with a unique name.
*
* <p>To reproduce the problem mentioned in gh-13491, delete the declaration
* of the {@code database-name} attribute of the embedded database in
* {@code datasource-config.xml} and run this <em>suite</em>.
*
* <h3>Solution</h3>
* <p>As of Spring 4.2, a proper solution is possible thanks to gh-13491.
* {@link TestClass2A} and {@link TestClass2B} both import
* {@code datasource-config-with-auto-generated-db-name.xml} which makes
* use of the new {@code generate-name} attribute of {@code <jdbc:embedded-database>}.
*
* @author Sam Brannen
* @author Mickael Leduque
*/
class GeneratedDatabaseNamesTests {
private static final String DATASOURCE_CONFIG_XML =
"classpath:/org/springframework/test/context/jdbc/datasource-config.xml";
private static final String DATASOURCE_CONFIG_WITH_AUTO_GENERATED_DB_NAME_XML =
"classpath:/org/springframework/test/context/jdbc/datasource-config-with-auto-generated-db-name.xml";
@Test
void runTestsWithGeneratedDatabaseNames() {
EngineTestKit.engine("junit-jupiter")
.selectors(
selectClass(TestClass1A.class),
selectClass(TestClass1B.class),
selectClass(TestClass2A.class),
selectClass(TestClass2B.class)
)
.execute()
.testEvents()
.assertStatistics(stats -> stats.started(4).succeeded(4).failed(0));
}
@ExtendWith(SpringExtension.class)
abstract static class AbstractTestCase {
@Resource
DataSource dataSource;
@Test
void test() {
assertThat(dataSource).isNotNull();
}
}
@ContextConfiguration
static class TestClass1A extends AbstractTestCase {
@Configuration
@ImportResource(DATASOURCE_CONFIG_XML)
static class Config {
}
}
@ContextConfiguration
static class TestClass1B extends AbstractTestCase {
@Configuration
@ImportResource(DATASOURCE_CONFIG_XML)
static class Config {
}
}
/**
* @since 4.2
*/
@ContextConfiguration
static class TestClass2A extends AbstractTestCase {
@Configuration
@ImportResource(DATASOURCE_CONFIG_WITH_AUTO_GENERATED_DB_NAME_XML)
static class Config {
}
}
/**
* @since 4.2
*/
@ContextConfiguration
static class TestClass2B extends AbstractTestCase {
@Configuration
@ImportResource(DATASOURCE_CONFIG_WITH_AUTO_GENERATED_DB_NAME_XML)
static class Config {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,23 +14,21 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr4868;
package org.springframework.test.context.jsr250;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import static org.assertj.core.api.Assertions.assertThat;
@@ -61,58 +59,56 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
@ContextConfiguration
public class Jsr250LifecycleTests {
class Jsr250LifecycleTests {
private final Log logger = LogFactory.getLog(Jsr250LifecycleTests.class);
@Configuration
static class Config {
@Bean
public LifecycleBean lifecycleBean() {
return new LifecycleBean();
}
}
@Autowired
private LifecycleBean lifecycleBean;
LifecycleBean lifecycleBean;
@PostConstruct
public void beforeAllTests() {
void beforeAllTests() {
logger.info("beforeAllTests()");
}
@PreDestroy
public void afterTestSuite() {
void afterTestSuite() {
logger.info("afterTestSuite()");
}
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() {
logger.info("setUp()");
}
@After
public void tearDown() {
@AfterEach
void tearDown() {
logger.info("tearDown()");
}
@Test
public void test1() {
void test1() {
logger.info("test1()");
assertThat(lifecycleBean).isNotNull();
}
@Test
public void test2() {
void test2() {
logger.info("test2()");
assertThat(lifecycleBean).isNotNull();
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
LifecycleBean lifecycleBean() {
return new LifecycleBean();
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr4868;
package org.springframework.test.context.jsr250;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,19 +14,19 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
package org.springframework.test.context.junit4;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests;
import org.springframework.test.context.annotation.PojoAndStringConfig;
/**
* Integration tests that verify support for configuration classes in
* the Spring TestContext Framework.
*
* <p>Furthermore, by extending {@link SpringJUnit4ClassRunnerAppCtxTests},
* <p>Furthermore, by extending {@code SpringJUnit4ClassRunnerAppCtxTests},
* this class also verifies support for several basic features of the
* Spring TestContext Framework. See JavaDoc in
* {@code SpringJUnit4ClassRunnerAppCtxTests} for details.
* {@link SpringJUnit4ClassRunnerAppCtxTests} for details.
*
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,27 +20,6 @@ import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.test.context.junit4.annotation.AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests;
import org.springframework.test.context.junit4.annotation.BeanOverridingDefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.BeanOverridingExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderDefaultConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderDefaultConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderExplicitConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.DefaultLoaderExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.annotation.ExplicitConfigClassesBaseTests;
import org.springframework.test.context.junit4.annotation.ExplicitConfigClassesInheritedTests;
import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests;
import org.springframework.test.context.junit4.profile.annotation.DefaultProfileAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.annotation.DevProfileAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.annotation.DevProfileResolverAnnotationConfigTests;
import org.springframework.test.context.junit4.profile.xml.DefaultProfileXmlConfigTests;
import org.springframework.test.context.junit4.profile.xml.DevProfileResolverXmlConfigTests;
import org.springframework.test.context.junit4.profile.xml.DevProfileXmlConfigTests;
/**
* JUnit test suite for tests involving {@link SpringRunner} and the
* <em>Spring TestContext Framework</em>; only intended to be run manually as a
@@ -64,24 +43,6 @@ StandardJUnit4FeaturesTests.class,//
StandardJUnit4FeaturesSpringRunnerTests.class,//
SpringJUnit47ClassRunnerRuleTests.class,//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
DefaultConfigClassesBaseTests.class,//
DefaultConfigClassesInheritedTests.class,//
BeanOverridingDefaultConfigClassesInheritedTests.class,//
ExplicitConfigClassesBaseTests.class,//
ExplicitConfigClassesInheritedTests.class,//
BeanOverridingExplicitConfigClassesInheritedTests.class,//
DefaultLoaderDefaultConfigClassesBaseTests.class,//
DefaultLoaderDefaultConfigClassesInheritedTests.class,//
DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.class,//
DefaultLoaderExplicitConfigClassesBaseTests.class,//
DefaultLoaderExplicitConfigClassesInheritedTests.class,//
DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests.class,//
DefaultProfileAnnotationConfigTests.class,//
DevProfileAnnotationConfigTests.class,//
DevProfileResolverAnnotationConfigTests.class,//
DefaultProfileXmlConfigTests.class,//
DevProfileXmlConfigTests.class,//
DevProfileResolverXmlConfigTests.class,//
ExpectedExceptionSpringRunnerTests.class,//
TimedSpringRunnerTests.class,//
RepeatedSpringRunnerTests.class,//
@@ -102,8 +63,7 @@ StandardJUnit4FeaturesTests.class,//
RollbackOverrideDefaultRollbackTrueTransactionalTests.class,//
RollbackOverrideDefaultRollbackFalseTransactionalTests.class,//
BeforeAndAfterTransactionAnnotationTests.class,//
TimedTransactionalSpringRunnerTests.class,//
HibernateSessionFlushingTests.class //
TimedTransactionalSpringRunnerTests.class//
})
public class SpringJUnit4TestSuite {
/* this test case consists entirely of tests loaded as a suite. */

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.aci;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.test.context.junit4.aci.annotation.InitializerWithoutConfigFilesOrClassesTests;
import org.springframework.test.context.junit4.aci.annotation.MergedInitializersAnnotationConfigTests;
import org.springframework.test.context.junit4.aci.annotation.MultipleInitializersAnnotationConfigTests;
import org.springframework.test.context.junit4.aci.annotation.OrderedInitializersAnnotationConfigTests;
import org.springframework.test.context.junit4.aci.annotation.OverriddenInitializersAnnotationConfigTests;
import org.springframework.test.context.junit4.aci.annotation.SingleInitializerAnnotationConfigTests;
import org.springframework.test.context.junit4.aci.xml.MultipleInitializersXmlConfigTests;
/**
* Convenience test suite for integration tests that verify support for
* {@link ApplicationContextInitializer ApplicationContextInitializers} (ACIs)
* in the TestContext framework.
*
* @author Sam Brannen
* @since 3.2
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
MultipleInitializersXmlConfigTests.class,//
SingleInitializerAnnotationConfigTests.class,//
MultipleInitializersAnnotationConfigTests.class,//
MergedInitializersAnnotationConfigTests.class,//
OverriddenInitializersAnnotationConfigTests.class,//
OrderedInitializersAnnotationConfigTests.class,//
InitializerWithoutConfigFilesOrClassesTests.class //
})
public class AciTestSuite {
}

View File

@@ -1,48 +0,0 @@
/*
* Copyright 2002-2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.annotation;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit test suite for annotation-driven <em>configuration class</em>
* support in the Spring TestContext Framework.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
DefaultConfigClassesBaseTests.class,//
DefaultConfigClassesInheritedTests.class,//
BeanOverridingDefaultConfigClassesInheritedTests.class,//
ExplicitConfigClassesBaseTests.class,//
ExplicitConfigClassesInheritedTests.class,//
BeanOverridingExplicitConfigClassesInheritedTests.class,//
DefaultLoaderDefaultConfigClassesBaseTests.class,//
DefaultLoaderDefaultConfigClassesInheritedTests.class,//
DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.class,//
DefaultLoaderExplicitConfigClassesBaseTests.class,//
DefaultLoaderExplicitConfigClassesInheritedTests.class,//
DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests.class //
})
public class AnnotationConfigTestSuite {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -79,7 +79,6 @@ public class SpringJUnit4ConcurrencyTests {
TimedTransactionalSpringRunnerTests.class,
// Web and Scopes
BasicAnnotationConfigWacSpringRuleTests.class,
// Spring MVC Test
};

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.annotation;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit test suite for <em>bean definition profile</em> support in the
* Spring TestContext Framework with annotation-based configuration.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
DefaultProfileAnnotationConfigTests.class,//
DevProfileAnnotationConfigTests.class,//
DevProfileResolverAnnotationConfigTests.class //
})
public class ProfileAnnotationConfigTestSuite {
}

View File

@@ -1,58 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.resolver;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Michail Nikolaev
* @since 4.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ActiveProfiles(resolver = ClassNameActiveProfilesResolver.class)
public class ClassNameActiveProfilesResolverTests {
@Configuration
static class Config {
}
@Autowired
private ApplicationContext applicationContext;
@Test
public void test() {
assertThat(Arrays.asList(applicationContext.getEnvironment().getActiveProfiles()).contains(
getClass().getSimpleName().toLowerCase())).isTrue();
}
}

View File

@@ -1,38 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.xml;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit test suite for <em>bean definition profile</em> support in the
* Spring TestContext Framework with XML-based configuration.
*
* @author Sam Brannen
* @since 3.1
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
DefaultProfileXmlConfigTests.class,//
DevProfileXmlConfigTests.class,//
DevProfileResolverXmlConfigTests.class //
})
public class ProfileXmlConfigTestSuite {
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,23 +18,30 @@ package org.springframework.test.context.junit4.rules;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.test.context.web.BasicAnnotationConfigWacTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.web.AbstractBasicWacTests;
import org.springframework.test.context.web.ServletContextAwareBean;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This class is an extension of {@link BasicAnnotationConfigWacTests}
* that has been modified to use {@link SpringClassRule} and
* {@link SpringMethodRule}.
* This class is a copy of {@link org.springframework.test.context.web.BasicAnnotationConfigWacTests}
* that has been modified to use the {@link JUnit4} runner combined with
* {@link SpringClassRule} and {@link SpringMethodRule}.
*
* @author Sam Brannen
* @since 4.2
*/
@RunWith(JUnit4.class)
public class BasicAnnotationConfigWacSpringRuleTests extends BasicAnnotationConfigWacTests {
// All tests are in superclass.
@ContextConfiguration
public class BasicAnnotationConfigWacSpringRuleTests extends AbstractBasicWacTests {
@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();
@@ -42,4 +49,46 @@ public class BasicAnnotationConfigWacSpringRuleTests extends BasicAnnotationConf
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Autowired
ServletContextAwareBean servletContextAwareBean;
/**
* Have to override this method to annotate it with JUnit 4's {@code @Test}
* annotation.
*/
@Test
@Override
public void basicWacFeatures() throws Exception {
super.basicWacFeatures();
}
@Test
public void fooEnigmaAutowired() {
assertThat(foo).isEqualTo("enigma");
}
@Test
public void servletContextAwareBeanProcessed() {
assertThat(servletContextAwareBean).isNotNull();
assertThat(servletContextAwareBean.getServletContext()).isNotNull();
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
String foo() {
return "enigma";
}
@Bean
ServletContextAwareBean servletContextAwareBean() {
return new ServletContextAwareBean();
}
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.bean.override.mockito.integration;
package org.springframework.test.context.junit4.rules;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -23,7 +23,6 @@ import org.junit.Test;
import org.springframework.test.annotation.Repeat;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.junit4.rules.SpringMethodRule;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

View File

@@ -1,49 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr3896;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* JUnit 4 based test suite for functionality proposed in <a
* href="https://opensource.atlassian.com/projects/spring/browse/SPR-3896"
* target="_blank">SPR-3896</a>.
*
* @author Sam Brannen
* @since 2.5
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({
DefaultLocationsBaseTests.class,
DefaultLocationsInheritedTests.class,
ExplicitLocationsBaseTests.class,
ExplicitLocationsInheritedTests.class,
BeanOverridingDefaultLocationsInheritedTests.class,
BeanOverridingExplicitLocationsInheritedTests.class
})
public class Spr3896TestSuite {
}

View File

@@ -1,50 +0,0 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr8849;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
/**
* Test suite to investigate claims raised in
* <a href="https://jira.spring.io/browse/SPR-8849">SPR-8849</a>.
*
* <h3>Work Around</h3>
* <p>By using a SpEL expression to generate a random {@code database-name}
* for the embedded database (see {@code datasource-config.xml}), we ensure
* that each {@code ApplicationContext} that imports the common configuration
* will create an embedded database with a unique name.
*
* <p>To reproduce the problem mentioned in SPR-8849, delete the declaration
* of the {@code database-name} attribute of the embedded database in
* {@code datasource-config.xml} and run this <em>suite</em>.
*
* <h3>Solution</h3>
* <p>As of Spring 4.2, a proper solution is possible thanks to SPR-8849.
* {@link TestClass3} and {@link TestClass4} both import
* {@code datasource-config-with-auto-generated-db-name.xml} which makes
* use of the new {@code generate-name} attribute of {@code <jdbc:embedded-database>}.
*
* @author Sam Brannen
* @since 3.2
*/
@RunWith(Suite.class)
@SuiteClasses({ TestClass1.class, TestClass2.class, TestClass3.class, TestClass4.class })
public class Spr8849Tests {
}

View File

@@ -1,60 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr8849;
import javax.sql.DataSource;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
*
* @author Mickael Leduque
* @author Sam Brannen
* @since 3.2
* @see Spr8849Tests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestClass1 {
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
assertThat(dataSource).isNotNull();
}
}

View File

@@ -1,60 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr8849;
import javax.sql.DataSource;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
*
* @author Mickael Leduque
* @author Sam Brannen
* @since 3.2
* @see Spr8849Tests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestClass2 {
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
assertThat(dataSource).isNotNull();
}
}

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr8849;
import javax.sql.DataSource;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
*
* @author Sam Brannen
* @since 4.2
* @see Spr8849Tests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestClass3 {
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config-with-auto-generated-db-name.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
assertThat(dataSource).isNotNull();
}
}

View File

@@ -1,59 +0,0 @@
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr8849;
import javax.sql.DataSource;
import jakarta.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This name of this class intentionally does not end with "Test" or "Tests"
* since it should only be run as part of the test suite: {@link Spr8849Tests}.
*
* @author Sam Brannen
* @since 4.2
* @see Spr8849Tests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestClass4 {
@Configuration
@ImportResource("classpath:/org/springframework/test/context/junit4/spr8849/datasource-config-with-auto-generated-db-name.xml")
static class Config {
}
@Resource
DataSource dataSource;
@Test
public void dummyTest() {
assertThat(dataSource).isNotNull();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.web;
package org.springframework.test.context.junit4.web;
import java.io.File;
@@ -30,6 +30,7 @@ import org.springframework.mock.web.MockHttpSession;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.ServletWebRequest;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,14 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
@@ -29,7 +28,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Transactional;
@@ -40,16 +39,16 @@ import static org.springframework.transaction.support.TransactionSynchronization
/**
* This set of tests (i.e., all concrete subclasses) investigates the claims made in
* <a href="https://jira.spring.io/browse/SPR-9051" target="_blank">SPR-9051</a>
* <a href="https://github.com/spring-projects/spring-framework/issues/13690" target="_blank">gh-13690</a>.
* with regard to transactional tests.
*
* @author Sam Brannen
* @since 3.2
* @see org.springframework.test.context.testng.AnnotationConfigTransactionalTestNGSpringContextTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringJUnitConfig
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public abstract class AbstractTransactionalAnnotatedConfigClassTests {
abstract class AbstractTransactionalAnnotatedConfigClassTests {
protected static final String JANE = "jane";
protected static final String SUE = "sue";
@@ -65,12 +64,12 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
@Autowired
public void setTransactionManager(DataSourceTransactionManager transactionManager) {
void setTransactionManager(DataSourceTransactionManager transactionManager) {
this.dataSourceFromTxManager = transactionManager.getDataSource();
}
@Autowired
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
this.dataSourceViaInjection = dataSource;
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@@ -96,38 +95,38 @@ public abstract class AbstractTransactionalAnnotatedConfigClassTests {
}
@Test
public void autowiringFromConfigClass() {
void autowiringFromConfigClass() {
assertThat(employee).as("The employee should have been autowired.").isNotNull();
assertThat(employee.getName()).isEqualTo("John Smith");
}
@BeforeTransaction
public void beforeTransaction() {
void beforeTransaction() {
assertNumRowsInPersonTable(0, "before a transactional test method");
assertAddPerson(YODA);
}
@Before
public void setUp() throws Exception {
@BeforeEach
void setUp() throws Exception {
assertNumRowsInPersonTable((isActualTransactionActive() ? 1 : 0), "before a test method");
}
@Test
@Transactional
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertAddPerson(JANE);
assertAddPerson(SUE);
assertNumRowsInPersonTable(3, "in modifyTestDataWithinTransaction()");
}
@After
public void tearDown() {
@AfterEach
void tearDown() {
assertNumRowsInPersonTable((isActualTransactionActive() ? 3 : 0), "after a test method");
}
@AfterTransaction
public void afterTransaction() {
void afterTransaction() {
assertThat(deletePerson(YODA)).as("Deleting yoda").isEqualTo(1);
assertNumRowsInPersonTable(0, "after a transactional test method");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,24 +14,22 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* This set of tests refutes the claims made in
* <a href="https://jira.spring.io/browse/SPR-9051" target="_blank">SPR-9051</a>.
* <a href="https://github.com/spring-projects/spring-framework/issues/13690" target="_blank">gh-13690</a>.
*
* <p><b>The Claims</b>:
*
@@ -48,9 +46,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Phillip Webb
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class)
public class AnnotatedConfigClassesWithoutAtConfigurationTests {
@SpringJUnitConfig(AnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class)
class AnnotatedConfigClassesWithoutAtConfigurationTests {
/**
* This is intentionally <b>not</b> annotated with {@code @Configuration}.
@@ -63,12 +60,12 @@ public class AnnotatedConfigClassesWithoutAtConfigurationTests {
@Bean
public String enigma() {
String enigma() {
return "enigma #" + enigmaCallCount.incrementAndGet();
}
@Bean
public LifecycleBean lifecycleBean() {
LifecycleBean lifecycleBean() {
// The following call to enigma() literally invokes the local
// enigma() method, not a CGLIB proxied version, since these methods
// are essentially factory bean methods.
@@ -87,7 +84,7 @@ public class AnnotatedConfigClassesWithoutAtConfigurationTests {
@Test
public void testSPR_9051() {
void testSPR_9051() {
assertThat(enigma).isNotNull();
assertThat(lifecycleBean).isNotNull();
assertThat(lifecycleBean.isInitialized()).isTrue();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,18 +14,16 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,9 +34,8 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.2
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AtBeanLiteModeScopeTests.LiteBeans.class)
public class AtBeanLiteModeScopeTests {
@SpringJUnitConfig(AtBeanLiteModeScopeTests.LiteBeans.class)
class AtBeanLiteModeScopeTests {
/**
* This is intentionally <b>not</b> annotated with {@code @Configuration}.
@@ -46,7 +43,7 @@ public class AtBeanLiteModeScopeTests {
static class LiteBeans {
@Bean
public LifecycleBean singleton() {
LifecycleBean singleton() {
LifecycleBean bean = new LifecycleBean("singleton");
assertThat(bean.isInitialized()).isFalse();
return bean;
@@ -54,7 +51,7 @@ public class AtBeanLiteModeScopeTests {
@Bean
@Scope("prototype")
public LifecycleBean prototype() {
LifecycleBean prototype() {
LifecycleBean bean = new LifecycleBean("prototype");
assertThat(bean.isInitialized()).isFalse();
return bean;
@@ -63,19 +60,19 @@ public class AtBeanLiteModeScopeTests {
@Autowired
private ApplicationContext applicationContext;
ApplicationContext applicationContext;
@Autowired
@Qualifier("singleton")
private LifecycleBean injectedSingletonBean;
LifecycleBean injectedSingletonBean;
@Autowired
@Qualifier("prototype")
private LifecycleBean injectedPrototypeBean;
LifecycleBean injectedPrototypeBean;
@Test
public void singletonLiteBean() {
void singletonLiteBean() {
assertThat(injectedSingletonBean).isNotNull();
assertThat(injectedSingletonBean.isInitialized()).isTrue();
@@ -87,7 +84,7 @@ public class AtBeanLiteModeScopeTests {
}
@Test
public void prototypeLiteBean() {
void prototypeLiteBean() {
assertThat(injectedPrototypeBean).isNotNull();
assertThat(injectedPrototypeBean.isInitialized()).isTrue();

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import jakarta.annotation.PostConstruct;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,11 +14,11 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
@@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @see TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests
*/
@ContextConfiguration
public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
AbstractTransactionalAnnotatedConfigClassTests {
/**
@@ -52,7 +52,7 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
static class Config {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
@@ -61,24 +61,24 @@ public class TransactionalAnnotatedConfigClassWithAtConfigurationTests extends
}
@Bean
public PlatformTransactionManager transactionManager() {
PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build();
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build();
}
}
@Before
public void compareDataSources() {
@BeforeEach
void compareDataSources() {
// NOTE: the two DataSource instances ARE the same!
assertThat(dataSourceViaInjection).isSameAs(dataSourceFromTxManager);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,11 +14,11 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.spr9051;
package org.springframework.test.context.litemode;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;
@@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @see TransactionalAnnotatedConfigClassWithAtConfigurationTests
*/
@ContextConfiguration(classes = TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class)
public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests extends
class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests extends
AbstractTransactionalAnnotatedConfigClassTests {
/**
@@ -58,7 +58,7 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
static class AnnotatedFactoryBeans {
@Bean
public Employee employee() {
Employee employee() {
Employee employee = new Employee();
employee.setName("John Smith");
employee.setAge(42);
@@ -67,7 +67,7 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
}
@Bean
public PlatformTransactionManager transactionManager() {
PlatformTransactionManager transactionManager() {
return new DataSourceTransactionManager(dataSource());
}
@@ -92,19 +92,19 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
* which is almost certainly not the desired or intended behavior.
*/
@Bean
public DataSource dataSource() {
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build();
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")//
// Ensure that this in-memory database is only used by this class:
.setName(getClass().getName())//
.build();
}
}
@Before
public void compareDataSources() {
@BeforeEach
void compareDataSources() {
// NOTE: the two DataSource instances are NOT the same!
assertThat(dataSourceViaInjection).isNotSameAs(dataSourceFromTxManager);
}
@@ -119,7 +119,7 @@ public class TransactionalAnnotatedConfigClassesWithoutAtConfigurationTests exte
*/
@AfterTransaction
@Override
public void afterTransaction() {
void afterTransaction() {
assertThat(deletePerson(YODA)).as("Deleting yoda").isEqualTo(1);
// NOTE: We would actually expect that there are now ZERO entries in the

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,21 +14,24 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm;
package org.springframework.test.context.orm.hibernate;
import javax.sql.DataSource;
import jakarta.persistence.PersistenceException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.exception.ConstraintViolationException;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.orm.domain.DriversLicense;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.junit4.orm.service.PersonService;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.orm.hibernate.domain.DriversLicense;
import org.springframework.test.context.orm.hibernate.domain.Person;
import org.springframework.test.context.orm.hibernate.service.PersonService;
import org.springframework.test.jdbc.JdbcTestUtils;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,23 +46,32 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @author Juergen Hoeller
* @author Vlad Mihalcea
* @since 3.0
* @see org.springframework.test.context.junit.jupiter.orm.JpaEntityListenerTests
* @see org.springframework.test.context.orm.jpa.JpaEntityListenerTests
*/
@ContextConfiguration
public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4SpringContextTests {
@SpringJUnitConfig
@Transactional
class HibernateSessionFlushingTests {
private static final String SAM = "Sam";
private static final String JUERGEN = "Juergen";
@Autowired
private PersonService personService;
JdbcTemplate jdbcTemplate;
@Autowired
private SessionFactory sessionFactory;
SessionFactory sessionFactory;
@Autowired
PersonService personService;
@Before
public void setup() {
@Autowired
void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@BeforeEach
void setup() {
assertThatTransaction().isActive();
assertThat(personService).as("PersonService should have been autowired.").isNotNull();
assertThat(sessionFactory).as("SessionFactory should have been autowired.").isNotNull();
@@ -67,7 +79,7 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
@Test
public void findSam() {
void findSam() {
Person sam = personService.findByName(SAM);
assertThat(sam).as("Should be able to find Sam").isNotNull();
DriversLicense driversLicense = sam.getDriversLicense();
@@ -77,7 +89,7 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
@Test // SPR-16956
@Transactional(readOnly = true)
public void findSamWithReadOnlySession() {
void findSamWithReadOnlySession() {
Person sam = personService.findByName(SAM);
sam.setName("Vlad");
// By setting setDefaultReadOnly(true), the user can no longer modify any entity...
@@ -88,7 +100,7 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
}
@Test
public void saveJuergenWithDriversLicense() {
void saveJuergenWithDriversLicense() {
DriversLicense driversLicense = new DriversLicense(2L, 2222L);
Person juergen = new Person(JUERGEN, driversLicense);
int numRows = countRowsInTable("person");
@@ -99,21 +111,21 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
}
@Test
public void saveJuergenWithNullDriversLicense() {
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(() ->
personService.save(new Person(JUERGEN)));
void saveJuergenWithNullDriversLicense() {
assertThatExceptionOfType(ConstraintViolationException.class)
.isThrownBy(() -> personService.save(new Person(JUERGEN)));
}
@Test
// no expected exception!
public void updateSamWithNullDriversLicenseWithoutSessionFlush() {
void updateSamWithNullDriversLicenseWithoutSessionFlush() {
updateSamWithNullDriversLicense();
// False positive, since an exception will be thrown once the session is
// finally flushed (i.e., in production code)
}
@Test
public void updateSamWithNullDriversLicenseWithSessionFlush() {
void updateSamWithNullDriversLicenseWithSessionFlush() {
updateSamWithNullDriversLicense();
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy(() -> {
// Manual flush is required to avoid false positive in test
@@ -134,4 +146,8 @@ public class HibernateSessionFlushingTests extends AbstractTransactionalJUnit4Sp
personService.save(sam);
}
private int countRowsInTable(String tableName) {
return JdbcTestUtils.countRowsInTable(this.jdbcTemplate, tableName);
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.domain;
package org.springframework.test.context.orm.hibernate.domain;
/**
* DriversLicense POJO.

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.domain;
package org.springframework.test.context.orm.hibernate.domain;
/**
* Person POJO.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,14 +14,12 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.repository.hibernate;
package org.springframework.test.context.orm.hibernate.repository;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.junit4.orm.repository.PersonRepository;
import org.springframework.test.context.orm.hibernate.domain.Person;
/**
* Hibernate implementation of the {@link PersonRepository} API.
@@ -30,12 +28,11 @@ import org.springframework.test.context.junit4.orm.repository.PersonRepository;
* @since 3.0
*/
@Repository
public class HibernatePersonRepository implements PersonRepository {
class HibernatePersonRepository implements PersonRepository {
private final SessionFactory sessionFactory;
@Autowired
public HibernatePersonRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@@ -48,8 +45,10 @@ public class HibernatePersonRepository implements PersonRepository {
@Override
public Person findByName(String name) {
return (Person) this.sessionFactory.getCurrentSession().createQuery(
"from Person person where person.name = :name").setParameter("name", name).getSingleResult();
return (Person) this.sessionFactory.getCurrentSession()
.createQuery("from Person person where person.name = :name")
.setParameter("name", name)
.getSingleResult();
}
}

View File

@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.repository;
package org.springframework.test.context.orm.hibernate.repository;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.orm.hibernate.domain.Person;
/**
* Person Repository API.

View File

@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.service;
package org.springframework.test.context.orm.hibernate.service;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.orm.hibernate.domain.Person;
/**
* Person Service API.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,11 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.orm.service.impl;
package org.springframework.test.context.orm.hibernate.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.orm.domain.Person;
import org.springframework.test.context.junit4.orm.repository.PersonRepository;
import org.springframework.test.context.junit4.orm.service.PersonService;
import org.springframework.test.context.orm.hibernate.domain.Person;
import org.springframework.test.context.orm.hibernate.repository.PersonRepository;
import org.springframework.transaction.annotation.Transactional;
/**
@@ -31,12 +29,11 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Service
@Transactional(readOnly = true)
public class StandardPersonService implements PersonService {
class StandardPersonService implements PersonService {
private final PersonRepository personRepository;
@Autowired
public StandardPersonService(PersonRepository personRepository) {
this.personRepository = personRepository;
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter.orm;
package org.springframework.test.context.orm.jpa;
import java.util.List;
@@ -37,10 +37,10 @@ import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.junit.jupiter.orm.domain.JpaPersonRepository;
import org.springframework.test.context.junit.jupiter.orm.domain.Person;
import org.springframework.test.context.junit.jupiter.orm.domain.PersonListener;
import org.springframework.test.context.junit.jupiter.orm.domain.PersonRepository;
import org.springframework.test.context.orm.jpa.domain.JpaPersonRepository;
import org.springframework.test.context.orm.jpa.domain.Person;
import org.springframework.test.context.orm.jpa.domain.PersonListener;
import org.springframework.test.context.orm.jpa.domain.PersonRepository;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
@@ -53,7 +53,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 5.3.18
* @see <a href="https://github.com/spring-projects/spring-framework/issues/28228">issue gh-28228</a>
* @see org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests
* @see org.springframework.test.context.orm.hibernate.HibernateSessionFlushingTests
*/
@SpringJUnitConfig
@Transactional

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter.orm.domain;
package org.springframework.test.context.orm.jpa.domain;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter.orm.domain;
package org.springframework.test.context.orm.jpa.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter.orm.domain;
package org.springframework.test.context.orm.jpa.domain;
import java.util.ArrayList;
import java.util.List;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit.jupiter.orm.domain;
package org.springframework.test.context.orm.jpa.domain;
/**
* Person repository API.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,16 +14,14 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.annotation;
package org.springframework.test.context.profile.annotation;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
@@ -32,25 +30,24 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Sam Brannen
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { DefaultProfileConfig.class, DevProfileConfig.class }, loader = AnnotationConfigContextLoader.class)
public class DefaultProfileAnnotationConfigTests {
@SpringJUnitConfig(classes = { DefaultProfileConfig.class, DevProfileConfig.class }, loader = AnnotationConfigContextLoader.class)
class DefaultProfileAnnotationConfigTests {
@Autowired
protected Pet pet;
Pet pet;
@Autowired(required = false)
protected Employee employee;
Employee employee;
@Test
public void pet() {
void pet() {
assertThat(pet).isNotNull();
assertThat(pet.getName()).isEqualTo("Fido");
}
@Test
public void employee() {
void employee() {
assertThat(employee).as("employee bean should not be created for the default profile").isNull();
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.annotation;
package org.springframework.test.context.profile.annotation;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.context.annotation.Bean;
@@ -24,7 +24,7 @@ import org.springframework.context.annotation.Configuration;
* @author Sam Brannen
* @since 3.1
*/
@Configuration
@Configuration(proxyBeanMethods = false)
public class DefaultProfileConfig {
@Bean

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,9 +14,9 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.annotation;
package org.springframework.test.context.profile.annotation;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.ActiveProfiles;
@@ -27,11 +27,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* @since 3.1
*/
@ActiveProfiles("dev")
public class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests {
class DevProfileAnnotationConfigTests extends DefaultProfileAnnotationConfigTests {
@Test
@Override
public void employee() {
void employee() {
assertThat(employee).as("employee bean should be loaded for the 'dev' profile").isNotNull();
assertThat(employee.getName()).isEqualTo("John Smith");
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.annotation;
package org.springframework.test.context.profile.annotation;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.context.annotation.Bean;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.importresource;
package org.springframework.test.context.profile.annotation;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ActiveProfilesResolver;
@@ -24,7 +24,7 @@ import org.springframework.test.context.ActiveProfilesResolver;
* @since 4.0
*/
@ActiveProfiles(resolver = DevProfileResolverAnnotationConfigTests.class, inheritProfiles = false)
public class DevProfileResolverAnnotationConfigTests extends DevProfileAnnotationConfigTests implements
class DevProfileResolverAnnotationConfigTests extends DevProfileAnnotationConfigTests implements
ActiveProfilesResolver {
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,16 +14,14 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4.profile.importresource;
package org.springframework.test.context.profile.importresource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.Pet;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,25 +29,24 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Juergen Hoeller
* @since 3.1
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = DefaultProfileConfig.class)
public class DefaultProfileAnnotationConfigTests {
@SpringJUnitConfig(DefaultProfileConfig.class)
class DefaultProfileAnnotationConfigTests {
@Autowired
protected Pet pet;
Pet pet;
@Autowired(required = false)
protected Employee employee;
Employee employee;
@Test
public void pet() {
void pet() {
assertThat(pet).isNotNull();
assertThat(pet.getName()).isEqualTo("Fido");
}
@Test
public void employee() {
void employee() {
assertThat(employee).as("employee bean should not be created for the default profile").isNull();
}

Some files were not shown because too many files have changed in this diff Show More