Merge branch '6.2.x'

This commit is contained in:
Sam Brannen 2025-04-26 07:55:00 +02:00
commit 4bf8e2f366
59 changed files with 534 additions and 617 deletions

View File

@ -167,7 +167,6 @@ class AotIntegrationTests extends AbstractAotTests {
void endToEndTestsForSelectedTestClasses() {
List<Class<?>> testClasses = List.of(
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests.class,
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
);

View File

@ -47,7 +47,7 @@ import static org.springframework.test.context.cache.ContextCacheTestUtils.reset
* @see ContextCacheTests
* @see LruContextCacheTests
*/
@SpringJUnitConfig(locations = "../junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
@SpringJUnitConfig(locations = "../config/CoreContextConfigurationAppCtxTests-context.xml")
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SpringExtensionContextCacheTests {

View File

@ -14,22 +14,22 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import org.springframework.test.context.ContextConfiguration;
/**
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
* we can specify an explicit, <em>absolute path</em> location for our
* application context.
*
* @author Sam Brannen
* @since 2.5
* @see SpringJUnit4ClassRunnerAppCtxTests
* @see ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
* @see CoreContextConfigurationAppCtxTests
* @see ClassPathResourceContextConfigurationAppCtxTests
* @see RelativePathContextConfigurationAppCtxTests
*/
@ContextConfiguration(locations = { SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
public class AbsolutePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
@ContextConfiguration(locations = CoreContextConfigurationAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH, inheritLocations = false)
class AbsolutePathContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
/* all tests are in the parent class. */
}

View File

@ -14,19 +14,18 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.annotation.PojoAndStringConfig;
/**
* Integration tests that verify support for configuration classes in
* the Spring TestContext Framework.
*
* <p>Furthermore, by extending {@code SpringJUnit4ClassRunnerAppCtxTests},
* <p>Furthermore, by extending {@link CoreContextConfigurationAppCtxTests},
* this class also verifies support for several basic features of the
* Spring TestContext Framework. See JavaDoc in
* {@link SpringJUnit4ClassRunnerAppCtxTests} for details.
* {@link CoreContextConfigurationAppCtxTests} for details.
*
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
*
@ -34,6 +33,6 @@ import org.springframework.test.context.annotation.PojoAndStringConfig;
* @since 3.1
*/
@ContextConfiguration(classes = PojoAndStringConfig.class, inheritLocations = false)
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
class AnnotationConfigContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
/* all tests are in the parent class. */
}

View File

@ -14,13 +14,17 @@
* limitations under the License.
*/
package org.springframework.test.context;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ -32,15 +36,16 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Chris Beams
* @since 3.0
*/
@SpringJUnitConfig
@ExtendWith(SpringExtension.class)
@ContextConfiguration
class AutowiredQualifierTests {
@Autowired
private String foo;
String foo;
@Autowired
@Qualifier("customFoo")
private String customFoo;
String customFoo;
@Test
@ -49,4 +54,19 @@ class AutowiredQualifierTests {
assertThat(customFoo).isEqualTo("custom");
}
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
String foo() {
return "normal";
}
@Bean
String customFoo() {
return "custom";
}
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

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,36 +14,35 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.util.ResourceUtils;
/**
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
* we can specify an explicit, <em>classpath</em> location for our application
* context.
*
* @author Sam Brannen
* @since 2.5
* @see SpringJUnit4ClassRunnerAppCtxTests
* @see CoreContextConfigurationAppCtxTests
* @see #CLASSPATH_CONTEXT_RESOURCE_PATH
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
* @see AbsolutePathContextConfigurationAppCtxTests
* @see RelativePathContextConfigurationAppCtxTests
*/
@ContextConfiguration(locations = { ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
public class ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
@ContextConfiguration(locations = { ClassPathResourceContextConfigurationAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
class ClassPathResourceContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
/**
* Classpath-based resource path for the application context configuration
* for {@link SpringJUnit4ClassRunnerAppCtxTests}:
* {@code &quot;classpath:/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;}
* for {@link CoreContextConfigurationAppCtxTests}: {@value}
*
* @see SpringJUnit4ClassRunnerAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
* @see CoreContextConfigurationAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
* @see ResourceUtils#CLASSPATH_URL_PREFIX
*/
public static final String CLASSPATH_CONTEXT_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX +
SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH;
CoreContextConfigurationAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH;
/* all tests are in the parent class. */

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.ClassOrderer;
import org.junit.platform.suite.api.ConfigurationParameter;
@ -44,11 +44,11 @@ import org.junit.platform.suite.api.Suite;
*/
@Suite
@IncludeEngines("junit-jupiter")
@SelectPackages("org.springframework.test.context.annotation")
@SelectPackages("org.springframework.test.context.config")
@IncludeClassNamePatterns(".*Tests$")
@ConfigurationParameter(
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
)
public class AnnotationConfigTestSuite {
public class ContextConfigTestSuite {
}

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,13 +14,13 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import jakarta.annotation.Resource;
import jakarta.inject.Inject;
import jakarta.inject.Named;
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.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
@ -33,18 +33,18 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
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.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.GenericXmlContextLoader;
import static org.assertj.core.api.Assertions.assertThat;
/**
* SpringJUnit4ClassRunnerAppCtxTests serves as a <em>proof of concept</em>
* JUnit 4 based test class, which verifies the expected functionality of
* {@link SpringRunner} in conjunction with the following:
* {@code CoreContextConfigurationAppCtxTests} serves as a <em>core</em> test class, which
* verifies the expected functionality of {@link ContextConfiguration @ContextConfiguration}
* in conjunction with the following:
*
* <ul>
* <li>{@link ContextConfiguration @ContextConfiguration}</li>
* <li>{@link Autowired @Autowired}</li>
* <li>{@link Qualifier @Qualifier}</li>
* <li>{@link Resource @Resource}</li>
@ -67,21 +67,24 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Sam Brannen
* @since 2.5
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
* @see InheritedConfigSpringJUnit4ClassRunnerAppCtxTests
* @see AbsolutePathContextConfigurationAppCtxTests
* @see AnnotationConfigContextConfigurationAppCtxTests
* @see ClassPathResourceContextConfigurationAppCtxTests
* @see InheritedConfigContextConfigurationAppCtxTests
* @see MultipleResourcesContextConfigurationAppCtxTests
* @see RelativePathContextConfigurationAppCtxTests
*/
@RunWith(SpringRunner.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAware, BeanNameAware, InitializingBean {
class CoreContextConfigurationAppCtxTests implements ApplicationContextAware, BeanNameAware, InitializingBean {
/**
* Default resource path for the application context configuration for
* {@link SpringJUnit4ClassRunnerAppCtxTests}: {@value}
* {@link CoreContextConfigurationAppCtxTests}: {@value}
*/
public static final String DEFAULT_CONTEXT_RESOURCE_PATH =
"/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
"/org/springframework/test/context/config/CoreContextConfigurationAppCtxTests-context.xml";
private Employee employee;
@ -136,12 +139,12 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Autowired
public void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
this.literalParameterValue = literalParameterValue;
}
@Autowired
public void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
this.spelParameterValue = spelParameterValue;
}
@ -162,23 +165,23 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
@Test
public void verifyBeanNameSet() {
void verifyBeanNameSet() {
assertThat(this.beanName).as("The bean name of this test instance should have been set due to BeanNameAware semantics.")
.startsWith(getClass().getName());
}
@Test
public void verifyApplicationContextSet() {
void verifyApplicationContextSet() {
assertThat(this.applicationContext).as("The application context should have been set due to ApplicationContextAware semantics.").isNotNull();
}
@Test
public void verifyBeanInitialized() {
void verifyBeanInitialized() {
assertThat(this.beanInitialized).as("This test bean should have been initialized due to InitializingBean semantics.").isTrue();
}
@Test
public void verifyAnnotationAutowiredAndInjectedFields() {
void verifyAnnotationAutowiredAndInjectedFields() {
assertThat(this.nonrequiredLong).as("The nonrequiredLong field should NOT have been autowired.").isNull();
assertThat(this.quux).as("The quux field should have been autowired via @Autowired and @Qualifier.").isEqualTo("Quux");
assertThat(this.namedQuux).as("The namedFoo field should have been injected via @Inject and @Named.").isEqualTo("Quux");
@ -192,13 +195,13 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public void verifyAnnotationAutowiredMethods() {
void verifyAnnotationAutowiredMethods() {
assertThat(this.employee).as("The employee setter method should have been autowired.").isNotNull();
assertThat(this.employee.getName()).isEqualTo("John Smith");
}
@Test
public void verifyAutowiredAtValueFields() {
void verifyAutowiredAtValueFields() {
assertThat(this.literalFieldValue).as("Literal @Value field should have been autowired").isNotNull();
assertThat(this.spelFieldValue).as("SpEL @Value field should have been autowired.").isNotNull();
assertThat(this.literalFieldValue).isEqualTo("enigma");
@ -206,7 +209,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public void verifyAutowiredAtValueMethods() {
void verifyAutowiredAtValueMethods() {
assertThat(this.literalParameterValue).as("Literal @Value method parameter should have been autowired.").isNotNull();
assertThat(this.spelParameterValue).as("SpEL @Value method parameter should have been autowired.").isNotNull();
assertThat(this.literalParameterValue).isEqualTo("enigma");
@ -214,12 +217,12 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
}
@Test
public void verifyResourceAnnotationInjectedFields() {
void verifyResourceAnnotationInjectedFields() {
assertThat(this.foo).as("The foo field should have been injected via @Resource.").isEqualTo("Foo");
}
@Test
public void verifyResourceAnnotationInjectedMethods() {
void verifyResourceAnnotationInjectedMethods() {
assertThat(this.bar).as("The bar method should have been wired via @Resource.").isEqualTo("Bar");
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.junit.jupiter.api.Test;

View File

@ -14,22 +14,22 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import java.lang.annotation.Inherited;
import org.springframework.test.context.ContextConfiguration;
/**
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests} which verifies that
* Extension of {@link CoreContextConfigurationAppCtxTests} which verifies that
* the configuration of an application context and dependency injection of a
* test instance function as expected within a class hierarchy, since
* {@link ContextConfiguration configuration} is {@link Inherited inherited}.
*
* @author Sam Brannen
* @since 2.5
* @see SpringJUnit4ClassRunnerAppCtxTests
* @see CoreContextConfigurationAppCtxTests
*/
public class InheritedConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
class InheritedConfigContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
/* all tests are in the parent class. */
}

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.
@ -14,33 +14,35 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.util.ResourceUtils;
/**
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
* we can specify multiple resource locations for our application context, each
* configured differently.
*
* <p>{@code MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests} is also used
* <p>{@code MultipleResourcesContextConfigurationAppCtxTests} is also used
* to verify support for the {@code value} attribute alias for
* {@code @ContextConfiguration}'s {@code locations} attribute.
*
* @author Sam Brannen
* @since 2.5
* @see SpringJUnit4ClassRunnerAppCtxTests
* @see CoreContextConfigurationAppCtxTests
*/
@ContextConfiguration( { MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_RESOURCE_PATH,
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.LOCAL_RESOURCE_PATH,
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.ABSOLUTE_RESOURCE_PATH })
public class MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
@ContextConfiguration( {
MultipleResourcesContextConfigurationAppCtxTests.CLASSPATH_RESOURCE_PATH,
MultipleResourcesContextConfigurationAppCtxTests.LOCAL_RESOURCE_PATH,
MultipleResourcesContextConfigurationAppCtxTests.ABSOLUTE_RESOURCE_PATH
})
class MultipleResourcesContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
public static final String CLASSPATH_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX +
"/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context1.xml";
public static final String LOCAL_RESOURCE_PATH = "MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context2.xml";
public static final String ABSOLUTE_RESOURCE_PATH = "/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context3.xml";
"/org/springframework/test/context/config/MultipleResourcesContextConfigurationAppCtxTests-context1.xml";
public static final String LOCAL_RESOURCE_PATH = "MultipleResourcesContextConfigurationAppCtxTests-context2.xml";
public static final String ABSOLUTE_RESOURCE_PATH = "/org/springframework/test/context/config/MultipleResourcesContextConfigurationAppCtxTests-context3.xml";
/* all tests are in the parent class. */

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,45 +14,42 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
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.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;
/**
* JUnit 4 based integration test which verifies that
* JUnit based integration test which verifies that
* {@link ContextConfiguration @ContextConfiguration} is optional.
*
* @author Phillip Webb
* @author Sam Brannen
* @since 4.3
*/
@RunWith(SpringRunner.class)
public class OptionalContextConfigurationSpringRunnerTests {
@Autowired
String foo;
@ExtendWith(SpringExtension.class)
class OptionalContextConfigurationTests {
@Test
public void contextConfigurationAnnotationIsOptional() {
assertThat(foo).isEqualTo("foo");
void contextConfigurationAnnotationIsOptional(@Autowired String foo) {
assertThat(foo).isEqualTo("bar");
}
@Configuration
@Configuration(proxyBeanMethods = false)
static class Config {
@Bean
String foo() {
return "foo";
return "bar";
}
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation;
package org.springframework.test.context.config;
import org.springframework.beans.testfixture.beans.Employee;
import org.springframework.beans.testfixture.beans.Pet;
@ -25,7 +25,7 @@ import org.springframework.context.annotation.Configuration;
* ApplicationContext configuration class for various integration tests.
*
* <p>The beans defined in this configuration class map directly to the
* beans defined in {@code SpringJUnit4ClassRunnerAppCtxTests-context.xml}.
* beans defined in {@code CoreContextConfigurationAppCtxTests-context.xml}.
* Consequently, the application contexts loaded from these two sources
* should be identical with regard to bean definitions.
*

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2007 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,21 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.config;
import org.springframework.test.context.ContextConfiguration;
/**
* Extension of {@link SpringJUnit4ClassRunnerAppCtxTests}, which verifies that
* Extension of {@link CoreContextConfigurationAppCtxTests}, which verifies that
* we can specify an explicit, <em>relative path</em> location for our
* application context.
*
* @author Sam Brannen
* @since 2.5
* @see SpringJUnit4ClassRunnerAppCtxTests
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
* @see CoreContextConfigurationAppCtxTests
* @see AbsolutePathContextConfigurationAppCtxTests
*/
@ContextConfiguration(locations = { "SpringJUnit4ClassRunnerAppCtxTests-context.xml" })
public class RelativePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
@ContextConfiguration(locations = "CoreContextConfigurationAppCtxTests-context.xml", inheritLocations = false)
class RelativePathContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
/* all tests are in the parent class. */
}

View File

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

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

View File

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

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

View File

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

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@ -22,8 +22,8 @@ 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.annotation.PojoAndStringConfig;
import org.springframework.test.context.annotation.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
import org.springframework.test.context.config.PojoAndStringConfig;
import org.springframework.test.context.config.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.assertj.core.api.Assertions.assertThat;

View File

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

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.annotation.meta;
package org.springframework.test.context.config.meta;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

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.
@ -38,26 +38,6 @@ import static org.assertj.core.api.SoftAssertions.assertSoftly;
*/
public class JUnitTestingUtils {
/**
* Run the tests in the supplied {@code testClass}, using the {@link Runner}
* configured via {@link RunWith @RunWith} or the default JUnit runner, and
* assert the expectations of the test execution.
*
* @param testClass the test class to run with JUnit
* @param expectedStartedCount the expected number of tests that started
* @param expectedFailedCount the expected number of tests that failed
* @param expectedFinishedCount the expected number of tests that finished
* @param expectedIgnoredCount the expected number of tests that were ignored
* @param expectedAssumptionFailedCount the expected number of tests that
* resulted in a failed assumption
*/
public static void runTestsAndAssertCounters(Class<?> testClass, int expectedStartedCount, int expectedFailedCount,
int expectedFinishedCount, int expectedIgnoredCount, int expectedAssumptionFailedCount) throws Exception {
runTestsAndAssertCounters(null, testClass, expectedStartedCount, expectedFailedCount, expectedFinishedCount,
expectedIgnoredCount, expectedAssumptionFailedCount);
}
/**
* Run the tests in the supplied {@code testClass}, using the specified
* {@link Runner}, and assert the expectations of the test execution.

View File

@ -1,80 +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;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* Extension of {@link DefaultRollbackFalseRollbackAnnotationTransactionalTests}
* which tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
* @author Sam Brannen
* @since 4.2
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackFalseRollbackAnnotationTransactionalTests extends
DefaultRollbackFalseRollbackAnnotationTransactionalTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
@Override
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
@Override
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
}
@Test
@Rollback
@Override
public void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(2);
}
@AfterClass
public static void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
}
}

View File

@ -1,77 +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;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* Extension of {@link DefaultRollbackTrueRollbackAnnotationTransactionalTests}
* which tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
* @author Sam Brannen
* @since 4.2
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackTrueRollbackAnnotationTransactionalTests extends
DefaultRollbackTrueRollbackAnnotationTransactionalTests {
private static JdbcTemplate jdbcTemplate;
@Autowired
@Override
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
@Override
public void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
}
@Test
@Rollback(false)
@Override
public void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(3);
}
@AfterClass
public static void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
}
}

View File

@ -38,32 +38,18 @@ import org.junit.runners.Suite.SuiteClasses;
*/
@RunWith(Suite.class)
// Note: the following 'multi-line' layout is for enhanced code readability.
@SuiteClasses({//
StandardJUnit4FeaturesTests.class,//
StandardJUnit4FeaturesSpringRunnerTests.class,//
SpringJUnit47ClassRunnerRuleTests.class,//
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
ExpectedExceptionSpringRunnerTests.class,//
TimedSpringRunnerTests.class,//
RepeatedSpringRunnerTests.class,//
EnabledAndIgnoredSpringRunnerTests.class,//
HardCodedProfileValueSourceSpringRunnerTests.class,//
SpringJUnit4ClassRunnerAppCtxTests.class,//
ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.class,//
AbsolutePathSpringJUnit4ClassRunnerAppCtxTests.class,//
RelativePathSpringJUnit4ClassRunnerAppCtxTests.class,//
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.class,//
InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
ParameterizedDependencyInjectionTests.class,//
ConcreteTransactionalJUnit4SpringContextTests.class,//
ClassLevelTransactionalSpringRunnerTests.class,//
MethodLevelTransactionalSpringRunnerTests.class,//
DefaultRollbackTrueRollbackAnnotationTransactionalTests.class,//
DefaultRollbackFalseRollbackAnnotationTransactionalTests.class,//
RollbackOverrideDefaultRollbackTrueTransactionalTests.class,//
RollbackOverrideDefaultRollbackFalseTransactionalTests.class,//
BeforeAndAfterTransactionAnnotationTests.class,//
TimedTransactionalSpringRunnerTests.class//
@SuiteClasses({
StandardJUnit4FeaturesTests.class,
StandardJUnit4FeaturesSpringRunnerTests.class,
SpringJUnit47ClassRunnerRuleTests.class,
ExpectedExceptionSpringRunnerTests.class,
TimedSpringRunnerTests.class,
RepeatedSpringRunnerTests.class,
EnabledAndIgnoredSpringRunnerTests.class,
HardCodedProfileValueSourceSpringRunnerTests.class,
ParameterizedDependencyInjectionTests.class,
ConcreteTransactionalJUnit4SpringContextTests.class,
TimedTransactionalSpringRunnerTests.class
})
public class SpringJUnit4TestSuite {
/* this test case consists entirely of tests loaded as a suite. */

View File

@ -38,7 +38,7 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @see org.springframework.test.context.junit.jupiter.transaction.TimedTransactionalSpringExtensionTests
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("transactionalTests-context.xml")
@ContextConfiguration("/org/springframework/test/context/transaction/transactionalTests-context.xml")
@Transactional
public class TimedTransactionalSpringRunnerTests {

View File

@ -25,10 +25,7 @@ import org.junit.Test;
import org.junit.experimental.ParallelComputer;
import org.springframework.core.testfixture.TestGroup;
import org.springframework.test.context.junit4.InheritedConfigSpringJUnit4ClassRunnerAppCtxTests;
import org.springframework.test.context.junit4.MethodLevelTransactionalSpringRunnerTests;
import org.springframework.test.context.junit4.SpringJUnit47ClassRunnerRuleTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.junit4.TimedTransactionalSpringRunnerTests;
import org.springframework.test.context.junit4.rules.BaseAppCtxRuleTests;
@ -70,12 +67,9 @@ public class SpringJUnit4ConcurrencyTests {
private final Class<?>[] testClasses = new Class<?>[] {
// Basics
SpringJUnit4ClassRunnerAppCtxTests.class,
InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,
SpringJUnit47ClassRunnerRuleTests.class,
BaseAppCtxRuleTests.class,
// Transactional
MethodLevelTransactionalSpringRunnerTests.class,
TimedTransactionalSpringRunnerTests.class,
// Web and Scopes
BasicAnnotationConfigWacSpringRuleTests.class,

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.
@ -16,23 +16,52 @@
package org.springframework.test.context.junit4.rules;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.test.context.junit4.BeforeAndAfterTransactionAnnotationTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* This class is an extension of {@link BeforeAndAfterTransactionAnnotationTests}
* that has been modified to use {@link SpringClassRule} and
* This class is a copy of {@code BeforeAndAfterTransactionAnnotationTests}
* that has been modified to use JUnit 4, {@link SpringClassRule}, and
* {@link SpringMethodRule}.
*
* @author Sam Brannen
* @since 4.2
*/
@RunWith(JUnit4.class)
public class BeforeAndAfterTransactionAnnotationSpringRuleTests extends BeforeAndAfterTransactionAnnotationTests {
@ContextConfiguration("/org/springframework/test/context/transaction/transactionalTests-context.xml")
@Transactional
public class BeforeAndAfterTransactionAnnotationSpringRuleTests {
private static final String JANE = "jane";
private static final String SUE = "sue";
private static final String LUKE = "luke";
private static final String LEIA = "leia";
private static final String YODA = "yoda";
private static int numBeforeTransactionCalls = 0;
private static int numAfterTransactionCalls = 0;
@ClassRule
public static final SpringClassRule springClassRule = new SpringClassRule();
@ -40,6 +69,116 @@ public class BeforeAndAfterTransactionAnnotationSpringRuleTests extends BeforeAn
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
// All tests are in superclass.
@Rule
public final TestName testName = new TestName();
static JdbcTemplate jdbcTemplate;
boolean inTransaction = false;
@Autowired
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@BeforeClass
public static void beforeClass() {
numBeforeTransactionCalls = 0;
numAfterTransactionCalls = 0;
}
@AfterClass
public static void afterClass() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
assertThat(numBeforeTransactionCalls).as("Verifying the total number of calls to beforeTransaction().").isEqualTo(2);
assertThat(numAfterTransactionCalls).as("Verifying the total number of calls to afterTransaction().").isEqualTo(2);
}
@BeforeTransaction
void beforeTransaction() {
assertThatTransaction().isNotActive();
this.inTransaction = true;
numBeforeTransactionCalls++;
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, YODA)).as("Adding yoda").isEqualTo(1);
}
@AfterTransaction
void afterTransaction() {
assertThatTransaction().isNotActive();
this.inTransaction = false;
numAfterTransactionCalls++;
assertThat(deletePerson(jdbcTemplate, YODA)).as("Deleting yoda").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table after a transactional test method.").isEqualTo(0);
}
@Before
public void before() {
assertShouldBeInTransaction();
long expected = (this.inTransaction ? 1 : 0);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table before a test method.").isEqualTo(expected);
}
@After
public void after() {
assertShouldBeInTransaction();
}
@Test
public void transactionalMethod1() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within transactionalMethod1().").isEqualTo(2);
}
@Test
public void transactionalMethod2() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within transactionalMethod2().").isEqualTo(3);
}
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void nonTransactionalMethod() {
assertThatTransaction().isNotActive();
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, YODA)).as("Adding yoda").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table without a transaction.").isEqualTo(3);
}
private void assertShouldBeInTransaction() {
boolean shouldBeInTransaction = !testName.getMethodName().equals("nonTransactionalMethod");
if (shouldBeInTransaction) {
assertThatTransaction().isActive();
}
else {
assertThatTransaction().isNotActive();
}
}
private static int clearPersonTable(JdbcTemplate jdbcTemplate) {
return jdbcTemplate.update("DELETE FROM person");
}
private static int countRowsInPersonTable(JdbcTemplate jdbcTemplate) {
return jdbcTemplate.queryForObject("SELECT COUNT(0) FROM person", Integer.class);
}
private static int addPerson(JdbcTemplate jdbcTemplate, String name) {
return jdbcTemplate.update("INSERT INTO person VALUES(?)", name);
}
private static int deletePerson(JdbcTemplate jdbcTemplate, String name) {
return jdbcTemplate.update("DELETE FROM person WHERE name=?", name);
}
}

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.
@ -43,22 +43,22 @@ public class SpringFailOnTimeoutTests {
@Test
public void nullNextStatement() {
assertThatIllegalArgumentException().isThrownBy(() ->
new SpringFailOnTimeout(null, 1));
assertThatIllegalArgumentException()
.isThrownBy(() -> new SpringFailOnTimeout(null, 1));
}
@Test
public void negativeTimeout() {
assertThatIllegalArgumentException().isThrownBy(() ->
new SpringFailOnTimeout(statement, -1));
assertThatIllegalArgumentException()
.isThrownBy(() -> new SpringFailOnTimeout(statement, -1));
}
@Test
public void userExceptionPropagates() throws Throwable {
willThrow(new Boom()).given(statement).evaluate();
assertThatExceptionOfType(Boom.class).isThrownBy(() ->
new SpringFailOnTimeout(statement, 1).evaluate());
assertThatExceptionOfType(Boom.class)
.isThrownBy(() -> new SpringFailOnTimeout(statement, 1).evaluate());
}
@Test
@ -68,8 +68,8 @@ public class SpringFailOnTimeoutTests {
return null;
}).given(statement).evaluate();
assertThatExceptionOfType(TimeoutException.class).isThrownBy(() ->
new SpringFailOnTimeout(statement, 1).evaluate());
assertThatExceptionOfType(TimeoutException.class)
.isThrownBy(() -> new SpringFailOnTimeout(statement, 1).evaluate());
}
@Test

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.
@ -14,27 +14,25 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.testng;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.List;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.testng.TestNG;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.testng.TrackingTestNGTestListener;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
/**
* Integration tests which verify that '<i>before</i>' and '<i>after</i>'
@ -50,62 +48,40 @@ import static org.assertj.core.api.Assertions.fail;
* @author Sam Brannen
* @since 2.5
*/
@RunWith(Parameterized.class)
public class FailingBeforeAndAfterMethodsTestNGTests {
class FailingBeforeAndAfterMethodsTestNGTests {
protected final Class<?> clazz;
protected final int expectedTestStartCount;
protected final int expectedTestSuccessCount;
protected final int expectedFailureCount;
protected final int expectedFailedConfigurationsCount;
@Parameters(name = "{0}")
public static Object[][] testData() {
return new Object[][] {
{ AlwaysFailingBeforeTestClassTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingAfterTestClassTestCase.class.getSimpleName(), 1, 1, 0, 1 },
{ AlwaysFailingPrepareTestInstanceTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingBeforeTestMethodTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ AlwaysFailingBeforeTestExecutionTestCase.class.getSimpleName(), 1, 0, 1, 0 },
{ AlwaysFailingAfterTestExecutionTestCase.class.getSimpleName(), 1, 0, 1, 0 },
{ AlwaysFailingAfterTestMethodTestCase.class.getSimpleName(), 1, 1, 0, 1 },
{ FailingBeforeTransactionTestCase.class.getSimpleName(), 1, 0, 0, 1 },
{ FailingAfterTransactionTestCase.class.getSimpleName(), 1, 1, 0, 1 }
};
}
public FailingBeforeAndAfterMethodsTestNGTests(String testClassName, int expectedTestStartCount,
@ParameterizedTest
@MethodSource("testData")
void runTestAndAssertCounters(Class<?> clazz, int expectedTestStartCount,
int expectedTestSuccessCount, int expectedFailureCount, int expectedFailedConfigurationsCount) throws Exception {
this.clazz = ClassUtils.forName(getClass().getName() + "." + testClassName, getClass().getClassLoader());
this.expectedTestStartCount = expectedTestStartCount;
this.expectedTestSuccessCount = expectedTestSuccessCount;
this.expectedFailureCount = expectedFailureCount;
this.expectedFailedConfigurationsCount = expectedFailedConfigurationsCount;
}
@Test
public void runTestAndAssertCounters() {
TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
TestNG testNG = new TestNG();
testNG.addListener(listener);
testNG.setTestClasses(new Class<?>[] {this.clazz});
testNG.setTestClasses(new Class<?>[] {clazz});
testNG.setVerbose(0);
testNG.run();
String name = this.clazz.getSimpleName();
String name = clazz.getSimpleName();
assertThat(listener.testStartCount).as("tests started for [" + name + "] ==> ").isEqualTo(this.expectedTestStartCount);
assertThat(listener.testSuccessCount).as("successful tests for [" + name + "] ==> ").isEqualTo(this.expectedTestSuccessCount);
assertThat(listener.testFailureCount).as("failed tests for [" + name + "] ==> ").isEqualTo(this.expectedFailureCount);
assertThat(listener.failedConfigurationsCount).as("failed configurations for [" + name + "] ==> ").isEqualTo(this.expectedFailedConfigurationsCount);
assertThat(listener.testStartCount).as("tests started for [" + name + "] ==> ").isEqualTo(expectedTestStartCount);
assertThat(listener.testSuccessCount).as("successful tests for [" + name + "] ==> ").isEqualTo(expectedTestSuccessCount);
assertThat(listener.testFailureCount).as("failed tests for [" + name + "] ==> ").isEqualTo(expectedFailureCount);
assertThat(listener.failedConfigurationsCount).as("failed configurations for [" + name + "] ==> ").isEqualTo(expectedFailedConfigurationsCount);
}
static List<Arguments> testData() {
return List.of(
argumentSet("AlwaysFailingBeforeTestClass", AlwaysFailingBeforeTestClassTestCase.class, 1, 0, 0, 1),
argumentSet("AlwaysFailingAfterTestClass", AlwaysFailingAfterTestClassTestCase.class, 1, 1, 0, 1),
argumentSet("AlwaysFailingPrepareTestInstance", AlwaysFailingPrepareTestInstanceTestCase.class, 1, 0, 0, 1),
argumentSet("AlwaysFailingBeforeTestMethod", AlwaysFailingBeforeTestMethodTestCase.class, 1, 0, 0, 1),
argumentSet("AlwaysFailingBeforeTestExecution", AlwaysFailingBeforeTestExecutionTestCase.class, 1, 0, 1, 0),
argumentSet("AlwaysFailingAfterTestExecution", AlwaysFailingAfterTestExecutionTestCase.class, 1, 0, 1, 0),
argumentSet("AlwaysFailingAfterTestMethod", AlwaysFailingAfterTestMethodTestCase.class, 1, 1, 0, 1),
argumentSet("FailingBeforeTransaction", FailingBeforeTransactionTestCase.class, 1, 0, 0, 1),
argumentSet("FailingAfterTransaction", FailingAfterTransactionTestCase.class, 1, 1, 0, 1)
);
}
@ -167,63 +143,63 @@ public class FailingBeforeAndAfterMethodsTestNGTests {
@TestExecutionListeners(inheritListeners = false)
public abstract static class BaseTestCase extends AbstractTestNGSpringContextTests {
abstract static class BaseTestCase extends AbstractTestNGSpringContextTests {
@org.testng.annotations.Test
public void testNothing() {
void testNothing() {
}
}
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
public static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
public static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
public static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
public static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
public static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
}
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
}
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
public static class FailingBeforeTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
static class FailingBeforeTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
@org.testng.annotations.Test
public void testNothing() {
void testNothing() {
}
@BeforeTransaction
public void beforeTransaction() {
void beforeTransaction() {
fail("always failing beforeTransaction()");
}
}
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
public static class FailingAfterTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
static class FailingAfterTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
@org.testng.annotations.Test
public void testNothing() {
void testNothing() {
}
@AfterTransaction
public void afterTransaction() {
void afterTransaction() {
fail("always failing afterTransaction()");
}
}

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,27 +14,24 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
import org.junit.runner.RunWith;
package org.springframework.test.context.transaction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
/**
* Abstract base class for verifying support of Spring's {@link Transactional
* &#64;Transactional} annotation.
* Abstract base class for verifying support of Spring's
* {@link Transactional @Transactional} annotation.
*
* @author Sam Brannen
* @since 2.5
* @see ClassLevelTransactionalSpringRunnerTests
* @see MethodLevelTransactionalSpringRunnerTests
* @see ClassLevelTransactionalSpringTests
* @see MethodLevelTransactionalSpringTests
* @see Transactional
*/
@RunWith(SpringRunner.class)
@ContextConfiguration("transactionalTests-context.xml")
public abstract class AbstractTransactionalSpringRunnerTests {
@SpringJUnitConfig(locations = "transactionalTests-context.xml")
abstract class AbstractTransactionalSpringTests {
protected static final String BOB = "bob";
protected static final String JANE = "jane";

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.
@ -14,22 +14,21 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@ -37,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* JUnit 4 based integration test which verifies
* JUnit based integration test which verifies
* {@link BeforeTransaction @BeforeTransaction} and
* {@link AfterTransaction @AfterTransaction} behavior.
*
@ -45,33 +44,31 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @since 2.5
*/
@Transactional
public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests {
@TestInstance(Lifecycle.PER_CLASS)
class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringTests {
protected static JdbcTemplate jdbcTemplate;
private static int numBeforeTransactionCalls = 0;
private static int numAfterTransactionCalls = 0;
protected static int numBeforeTransactionCalls = 0;
protected static int numAfterTransactionCalls = 0;
JdbcTemplate jdbcTemplate;
protected boolean inTransaction = false;
@Rule
public final TestName testName = new TestName();
boolean inTransaction = false;
@Autowired
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@BeforeClass
public static void beforeClass() {
@BeforeAll
void beforeClass() {
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls = 0;
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls = 0;
}
@AfterClass
public static void afterClass() {
@AfterAll
void afterClass() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
assertThat(BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls).as("Verifying the total number of calls to beforeTransaction().").isEqualTo(2);
assertThat(BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls).as("Verifying the total number of calls to afterTransaction().").isEqualTo(2);
@ -95,37 +92,27 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table after a transactional test method.").isEqualTo(0);
}
@Before
public void before() {
assertShouldBeInTransaction();
@BeforeEach
void before(TestInfo testInfo) {
assertShouldBeInTransaction(testInfo);
long expected = (this.inTransaction ? 1 : 0);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table before a test method.").isEqualTo(expected);
}
private void assertShouldBeInTransaction() {
boolean shouldBeInTransaction = !testName.getMethodName().equals("nonTransactionalMethod");
if (shouldBeInTransaction) {
assertThatTransaction().isActive();
}
else {
assertThatTransaction().isNotActive();
}
}
@After
public void after() {
assertShouldBeInTransaction();
@AfterEach
void after(TestInfo testInfo) {
assertShouldBeInTransaction(testInfo);
}
@Test
public void transactionalMethod1() {
void transactionalMethod1() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within transactionalMethod1().").isEqualTo(2);
}
@Test
public void transactionalMethod2() {
void transactionalMethod2() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
@ -134,7 +121,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void nonTransactionalMethod() {
void nonTransactionalMethod() {
assertThatTransaction().isNotActive();
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").isEqualTo(1);
@ -142,4 +129,14 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table without a transaction.").isEqualTo(3);
}
private static void assertShouldBeInTransaction(TestInfo testInfo) {
if (!testInfo.getTestMethod().get().getName().equals("nonTransactionalMethod")) {
assertThatTransaction().isActive();
}
else {
assertThatTransaction().isNotActive();
}
}
}

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,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
@ -29,7 +31,6 @@ import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@ -37,12 +38,11 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* JUnit 4 based integration test which verifies support of Spring's
* {@link Transactional &#64;Transactional}, {@link TestExecutionListeners
* &#64;TestExecutionListeners}, and {@link ContextConfiguration
* &#64;ContextConfiguration} annotations in conjunction with the
* {@link SpringRunner} and the following
* {@link TestExecutionListener TestExecutionListeners}:
* JUnit based integration test which verifies support of Spring's
* {@link Transactional @Transactional},
* {@link TestExecutionListeners @TestExecutionListeners}, and
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
* with the following {@link TestExecutionListener TestExecutionListeners}:
*
* <ul>
* <li>{@link DependencyInjectionTestExecutionListener}</li>
@ -55,33 +55,29 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
*
* @author Sam Brannen
* @since 2.5
* @see MethodLevelTransactionalSpringRunnerTests
* @see MethodLevelTransactionalSpringTests
*/
@Transactional
public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
@TestInstance(Lifecycle.PER_CLASS)
class ClassLevelTransactionalSpringTests extends AbstractTransactionalSpringTests {
protected static JdbcTemplate jdbcTemplate;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@AfterClass
public static void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
}
@Before
public void verifyInitialTestData() {
@BeforeEach
void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
}
@Test
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
@ -91,7 +87,7 @@ public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactio
@Test
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void modifyTestDataWithoutTransaction() {
void modifyTestDataWithoutTransaction() {
assertThatTransaction().isNotActive();
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").isEqualTo(1);
@ -99,4 +95,10 @@ public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactio
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table without a transaction.").isEqualTo(4);
}
@AfterAll
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
}
}

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,19 +14,21 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,32 +45,32 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @since 4.2
* @see Rollback
* @see Transactional#transactionManager
* @see DefaultRollbackFalseTransactionalTests
* @see DefaultRollbackTrueRollbackAnnotationTransactionalTests
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
@SpringJUnitConfig(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
@Transactional("txMgr")
@Rollback(false)
public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
@TestInstance(Lifecycle.PER_CLASS)
class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringTests {
private static JdbcTemplate jdbcTemplate;
JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
@BeforeEach
void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
}
@Test
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
@ -76,8 +78,8 @@ public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends Ab
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(2);
}
@AfterClass
public static void verifyFinalTestData() {
@AfterAll
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(2);
}

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,19 +14,21 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.Timeout;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,42 +45,43 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @since 4.2
* @see Rollback
* @see Transactional#transactionManager
* @see DefaultRollbackTrueTransactionalTests
* @see DefaultRollbackFalseRollbackAnnotationTransactionalTests
*/
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
@SpringJUnitConfig(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
@Transactional("txMgr")
@Rollback(true)
public class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
@TestInstance(Lifecycle.PER_CLASS)
class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
@BeforeEach
void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
}
@Test(timeout = 1000)
public void modifyTestDataWithinTransaction() {
@Test
@Timeout(1)
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(3);
}
@AfterClass
public static void verifyFinalTestData() {
@AfterAll
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
}

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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
@ -32,15 +32,15 @@ import org.springframework.transaction.PlatformTransactionManager;
* @since 4.2
*/
@Configuration
public class EmbeddedPersonDatabaseTestsConfig {
class EmbeddedPersonDatabaseTestsConfig {
@Bean
public PlatformTransactionManager txMgr() {
PlatformTransactionManager txMgr() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(true)
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")

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,15 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@ -30,19 +32,17 @@ import org.springframework.test.context.TestExecutionListener;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
/**
* JUnit 4 based integration test which verifies support of Spring's
* {@link Transactional &#64;Transactional}, {@link TestExecutionListeners
* &#64;TestExecutionListeners}, and {@link ContextConfiguration
* &#64;ContextConfiguration} annotations in conjunction with the
* {@link SpringRunner} and the following
* {@link TestExecutionListener TestExecutionListeners}:
* JUnit based integration test which verifies support of Spring's
* {@link Transactional @Transactional},
* {@link TestExecutionListeners @TestExecutionListeners}, and
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
* with the following {@link TestExecutionListener TestExecutionListeners}:
*
* <ul>
* <li>{@link DependencyInjectionTestExecutionListener}</li>
@ -55,28 +55,24 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
*
* @author Sam Brannen
* @since 2.5
* @see ClassLevelTransactionalSpringRunnerTests
* @see ClassLevelTransactionalSpringTests
*/
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class })
public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
@TestInstance(Lifecycle.PER_CLASS)
class MethodLevelTransactionalSpringTests extends AbstractTransactionalSpringTests {
protected static JdbcTemplate jdbcTemplate;
private JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("dataSource2")
public void setDataSource(DataSource dataSource) {
void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@AfterClass
public static void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
}
@Before
public void verifyInitialTestData() {
@BeforeEach
void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
@ -84,7 +80,7 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
@Test
@Transactional("transactionManager2")
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
@ -93,7 +89,7 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
}
@Test
public void modifyTestDataWithoutTransaction() {
void modifyTestDataWithoutTransaction() {
assertThatTransaction().isNotActive();
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").isEqualTo(1);
@ -101,4 +97,10 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table without a transaction.").isEqualTo(4);
}
@AfterAll
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
}
}

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,12 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import static org.assertj.core.api.Assertions.assertThat;
@ -38,23 +34,15 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @since 2.5
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackFalseTransactionalTests
class RollbackOverrideDefaultRollbackFalseTransactionalTests
extends DefaultRollbackFalseRollbackAnnotationTransactionalTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
@Autowired
@BeforeEach
@Override
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
@Override
public void verifyInitialTestData() {
void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
@ -63,7 +51,7 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalTests
@Test
@Rollback
@Override
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
@ -71,8 +59,9 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalTests
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(2);
}
@AfterClass
public static void verifyFinalTestData() {
@AfterAll
@Override
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
}

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,12 @@
* limitations under the License.
*/
package org.springframework.test.context.junit4;
package org.springframework.test.context.transaction;
import javax.sql.DataSource;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import static org.assertj.core.api.Assertions.assertThat;
@ -38,21 +34,12 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
* @since 2.5
* @see Rollback
*/
public class RollbackOverrideDefaultRollbackTrueTransactionalTests
class RollbackOverrideDefaultRollbackTrueTransactionalTests
extends DefaultRollbackTrueRollbackAnnotationTransactionalTests {
private static JdbcTemplate jdbcTemplate;
@BeforeEach
@Override
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
@Override
public void verifyInitialTestData() {
void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
@ -61,15 +48,16 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalTests
@Test
@Rollback(false)
@Override
public void modifyTestDataWithinTransaction() {
void modifyTestDataWithinTransaction() {
assertThatTransaction().isActive();
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(3);
}
@AfterClass
public static void verifyFinalTestData() {
@AfterAll
@Override
void verifyFinalTestData() {
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
}

View File

@ -4,7 +4,7 @@
xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="transactionalTests-context.xml" />
<import resource="classpath:/org/springframework/test/context/transaction/transactionalTests-context.xml" />
<jdbc:initialize-database data-source="dataSource" >
<jdbc:script location="classpath:/org/springframework/test/jdbc/data.sql"/>