Migrate remaining JUnit 4 tests to JUnit Jupiter where feasible
In 49e5c84928
I unfortunately overlooked
several JUnit 4 based tests in the `junit4` package that should be
migrated to JUnit Jupiter.
This commit address those remaining test classes.
See gh-23451
See gh-34794
Closes gh-34813
This commit is contained in:
parent
c48ff357dc
commit
176b0b09bf
|
@ -167,7 +167,6 @@ class AotIntegrationTests extends AbstractAotTests {
|
||||||
void endToEndTestsForSelectedTestClasses() {
|
void endToEndTestsForSelectedTestClasses() {
|
||||||
List<Class<?>> testClasses = List.of(
|
List<Class<?>> testClasses = List.of(
|
||||||
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
|
org.springframework.test.context.bean.override.easymock.EasyMockBeanIntegrationTests.class,
|
||||||
org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests.class,
|
|
||||||
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
|
org.springframework.test.context.junit4.ParameterizedDependencyInjectionTests.class
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -47,7 +47,7 @@ import static org.springframework.test.context.cache.ContextCacheTestUtils.reset
|
||||||
* @see ContextCacheTests
|
* @see ContextCacheTests
|
||||||
* @see LruContextCacheTests
|
* @see LruContextCacheTests
|
||||||
*/
|
*/
|
||||||
@SpringJUnitConfig(locations = "../junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml")
|
@SpringJUnitConfig(locations = "../config/CoreContextConfigurationAppCtxTests-context.xml")
|
||||||
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
|
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
|
||||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||||
class SpringExtensionContextCacheTests {
|
class SpringExtensionContextCacheTests {
|
||||||
|
|
|
@ -14,22 +14,22 @@
|
||||||
* limitations under the License.
|
* 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.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
|
* we can specify an explicit, <em>absolute path</em> location for our
|
||||||
* application context.
|
* application context.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
* @see CoreContextConfigurationAppCtxTests
|
||||||
* @see ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests
|
* @see ClassPathResourceContextConfigurationAppCtxTests
|
||||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see RelativePathContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(locations = { SpringJUnit4ClassRunnerAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
|
@ContextConfiguration(locations = CoreContextConfigurationAppCtxTests.DEFAULT_CONTEXT_RESOURCE_PATH, inheritLocations = false)
|
||||||
public class AbsolutePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
class AbsolutePathContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
/* all tests are in the parent class. */
|
/* all tests are in the parent class. */
|
||||||
}
|
}
|
|
@ -14,19 +14,18 @@
|
||||||
* limitations under the License.
|
* 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.ContextConfiguration;
|
||||||
import org.springframework.test.context.annotation.PojoAndStringConfig;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Integration tests that verify support for configuration classes in
|
* Integration tests that verify support for configuration classes in
|
||||||
* the Spring TestContext Framework.
|
* 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
|
* this class also verifies support for several basic features of the
|
||||||
* Spring TestContext Framework. See JavaDoc in
|
* Spring TestContext Framework. See JavaDoc in
|
||||||
* {@link SpringJUnit4ClassRunnerAppCtxTests} for details.
|
* {@link CoreContextConfigurationAppCtxTests} for details.
|
||||||
*
|
*
|
||||||
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
|
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
|
||||||
*
|
*
|
||||||
|
@ -34,6 +33,6 @@ import org.springframework.test.context.annotation.PojoAndStringConfig;
|
||||||
* @since 3.1
|
* @since 3.1
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(classes = PojoAndStringConfig.class, inheritLocations = false)
|
@ContextConfiguration(classes = PojoAndStringConfig.class, inheritLocations = false)
|
||||||
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
class AnnotationConfigContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
/* all tests are in the parent class. */
|
/* all tests are in the parent class. */
|
||||||
}
|
}
|
|
@ -14,13 +14,17 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
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;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
@ -32,15 +36,16 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
@SpringJUnitConfig
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@ContextConfiguration
|
||||||
class AutowiredQualifierTests {
|
class AutowiredQualifierTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private String foo;
|
String foo;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("customFoo")
|
@Qualifier("customFoo")
|
||||||
private String customFoo;
|
String customFoo;
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -49,4 +54,19 @@ class AutowiredQualifierTests {
|
||||||
assertThat(customFoo).isEqualTo("custom");
|
assertThat(customFoo).isEqualTo("custom");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class Config {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
String foo() {
|
||||||
|
return "normal";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
String customFoo() {
|
||||||
|
return "custom";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,36 +14,35 @@
|
||||||
* limitations under the License.
|
* 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.ContextConfiguration;
|
||||||
import org.springframework.util.ResourceUtils;
|
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
|
* we can specify an explicit, <em>classpath</em> location for our application
|
||||||
* context.
|
* context.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
* @see CoreContextConfigurationAppCtxTests
|
||||||
* @see #CLASSPATH_CONTEXT_RESOURCE_PATH
|
* @see #CLASSPATH_CONTEXT_RESOURCE_PATH
|
||||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see AbsolutePathContextConfigurationAppCtxTests
|
||||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see RelativePathContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(locations = { ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
|
@ContextConfiguration(locations = { ClassPathResourceContextConfigurationAppCtxTests.CLASSPATH_CONTEXT_RESOURCE_PATH }, inheritLocations = false)
|
||||||
public class ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
class ClassPathResourceContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Classpath-based resource path for the application context configuration
|
* Classpath-based resource path for the application context configuration
|
||||||
* for {@link SpringJUnit4ClassRunnerAppCtxTests}:
|
* for {@link CoreContextConfigurationAppCtxTests}: {@value}
|
||||||
* {@code "classpath:/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml"}
|
|
||||||
*
|
*
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
|
* @see CoreContextConfigurationAppCtxTests#DEFAULT_CONTEXT_RESOURCE_PATH
|
||||||
* @see ResourceUtils#CLASSPATH_URL_PREFIX
|
* @see ResourceUtils#CLASSPATH_URL_PREFIX
|
||||||
*/
|
*/
|
||||||
public static final String CLASSPATH_CONTEXT_RESOURCE_PATH = 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. */
|
/* all tests are in the parent class. */
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.jupiter.api.ClassOrderer;
|
||||||
import org.junit.platform.suite.api.ConfigurationParameter;
|
import org.junit.platform.suite.api.ConfigurationParameter;
|
||||||
|
@ -44,11 +44,11 @@ import org.junit.platform.suite.api.Suite;
|
||||||
*/
|
*/
|
||||||
@Suite
|
@Suite
|
||||||
@IncludeEngines("junit-jupiter")
|
@IncludeEngines("junit-jupiter")
|
||||||
@SelectPackages("org.springframework.test.context.annotation")
|
@SelectPackages("org.springframework.test.context.config")
|
||||||
@IncludeClassNamePatterns(".*Tests$")
|
@IncludeClassNamePatterns(".*Tests$")
|
||||||
@ConfigurationParameter(
|
@ConfigurationParameter(
|
||||||
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
|
key = ClassOrderer.DEFAULT_ORDER_PROPERTY_NAME,
|
||||||
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
|
value = "org.junit.jupiter.api.ClassOrderer$ClassName"
|
||||||
)
|
)
|
||||||
public class AnnotationConfigTestSuite {
|
public class ContextConfigTestSuite {
|
||||||
}
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,13 +14,13 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import jakarta.inject.Named;
|
import jakarta.inject.Named;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanNameAware;
|
import org.springframework.beans.factory.BeanNameAware;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
@ -33,18 +33,18 @@ import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.context.ApplicationContextAware;
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.TestExecutionListeners;
|
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.DependencyInjectionTestExecutionListener;
|
||||||
import org.springframework.test.context.support.GenericXmlContextLoader;
|
import org.springframework.test.context.support.GenericXmlContextLoader;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* SpringJUnit4ClassRunnerAppCtxTests serves as a <em>proof of concept</em>
|
* {@code CoreContextConfigurationAppCtxTests} serves as a <em>core</em> test class, which
|
||||||
* JUnit 4 based test class, which verifies the expected functionality of
|
* verifies the expected functionality of {@link ContextConfiguration @ContextConfiguration}
|
||||||
* {@link SpringRunner} in conjunction with the following:
|
* in conjunction with the following:
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link ContextConfiguration @ContextConfiguration}</li>
|
|
||||||
* <li>{@link Autowired @Autowired}</li>
|
* <li>{@link Autowired @Autowired}</li>
|
||||||
* <li>{@link Qualifier @Qualifier}</li>
|
* <li>{@link Qualifier @Qualifier}</li>
|
||||||
* <li>{@link Resource @Resource}</li>
|
* <li>{@link Resource @Resource}</li>
|
||||||
|
@ -67,21 +67,24 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see AbsolutePathContextConfigurationAppCtxTests
|
||||||
* @see RelativePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see AnnotationConfigContextConfigurationAppCtxTests
|
||||||
* @see InheritedConfigSpringJUnit4ClassRunnerAppCtxTests
|
* @see ClassPathResourceContextConfigurationAppCtxTests
|
||||||
|
* @see InheritedConfigContextConfigurationAppCtxTests
|
||||||
|
* @see MultipleResourcesContextConfigurationAppCtxTests
|
||||||
|
* @see RelativePathContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
@TestExecutionListeners(DependencyInjectionTestExecutionListener.class)
|
@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
|
* Default resource path for the application context configuration for
|
||||||
* {@link SpringJUnit4ClassRunnerAppCtxTests}: {@value}
|
* {@link CoreContextConfigurationAppCtxTests}: {@value}
|
||||||
*/
|
*/
|
||||||
public static final String DEFAULT_CONTEXT_RESOURCE_PATH =
|
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;
|
private Employee employee;
|
||||||
|
@ -136,12 +139,12 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
|
void setLiteralParameterValue(@Value("enigma") String literalParameterValue) {
|
||||||
this.literalParameterValue = literalParameterValue;
|
this.literalParameterValue = literalParameterValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
|
void setSpelParameterValue(@Value("#{2 == (1+1)}") Boolean spelParameterValue) {
|
||||||
this.spelParameterValue = spelParameterValue;
|
this.spelParameterValue = spelParameterValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,23 +165,23 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@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.")
|
assertThat(this.beanName).as("The bean name of this test instance should have been set due to BeanNameAware semantics.")
|
||||||
.startsWith(getClass().getName());
|
.startsWith(getClass().getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyApplicationContextSet() {
|
void verifyApplicationContextSet() {
|
||||||
assertThat(this.applicationContext).as("The application context should have been set due to ApplicationContextAware semantics.").isNotNull();
|
assertThat(this.applicationContext).as("The application context should have been set due to ApplicationContextAware semantics.").isNotNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyBeanInitialized() {
|
void verifyBeanInitialized() {
|
||||||
assertThat(this.beanInitialized).as("This test bean should have been initialized due to InitializingBean semantics.").isTrue();
|
assertThat(this.beanInitialized).as("This test bean should have been initialized due to InitializingBean semantics.").isTrue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyAnnotationAutowiredAndInjectedFields() {
|
void verifyAnnotationAutowiredAndInjectedFields() {
|
||||||
assertThat(this.nonrequiredLong).as("The nonrequiredLong field should NOT have been autowired.").isNull();
|
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.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");
|
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
|
@Test
|
||||||
public void verifyAnnotationAutowiredMethods() {
|
void verifyAnnotationAutowiredMethods() {
|
||||||
assertThat(this.employee).as("The employee setter method should have been autowired.").isNotNull();
|
assertThat(this.employee).as("The employee setter method should have been autowired.").isNotNull();
|
||||||
assertThat(this.employee.getName()).isEqualTo("John Smith");
|
assertThat(this.employee.getName()).isEqualTo("John Smith");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyAutowiredAtValueFields() {
|
void verifyAutowiredAtValueFields() {
|
||||||
assertThat(this.literalFieldValue).as("Literal @Value field should have been autowired").isNotNull();
|
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.spelFieldValue).as("SpEL @Value field should have been autowired.").isNotNull();
|
||||||
assertThat(this.literalFieldValue).isEqualTo("enigma");
|
assertThat(this.literalFieldValue).isEqualTo("enigma");
|
||||||
|
@ -206,7 +209,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyAutowiredAtValueMethods() {
|
void verifyAutowiredAtValueMethods() {
|
||||||
assertThat(this.literalParameterValue).as("Literal @Value method parameter should have been autowired.").isNotNull();
|
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.spelParameterValue).as("SpEL @Value method parameter should have been autowired.").isNotNull();
|
||||||
assertThat(this.literalParameterValue).isEqualTo("enigma");
|
assertThat(this.literalParameterValue).isEqualTo("enigma");
|
||||||
|
@ -214,12 +217,12 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyResourceAnnotationInjectedFields() {
|
void verifyResourceAnnotationInjectedFields() {
|
||||||
assertThat(this.foo).as("The foo field should have been injected via @Resource.").isEqualTo("Foo");
|
assertThat(this.foo).as("The foo field should have been injected via @Resource.").isEqualTo("Foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void verifyResourceAnnotationInjectedMethods() {
|
void verifyResourceAnnotationInjectedMethods() {
|
||||||
assertThat(this.bar).as("The bar method should have been wired via @Resource.").isEqualTo("Bar");
|
assertThat(this.bar).as("The bar method should have been wired via @Resource.").isEqualTo("Bar");
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.annotation;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
|
@ -14,22 +14,22 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import java.lang.annotation.Inherited;
|
import java.lang.annotation.Inherited;
|
||||||
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
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
|
* the configuration of an application context and dependency injection of a
|
||||||
* test instance function as expected within a class hierarchy, since
|
* test instance function as expected within a class hierarchy, since
|
||||||
* {@link ContextConfiguration configuration} is {@link Inherited inherited}.
|
* {@link ContextConfiguration configuration} is {@link Inherited inherited}.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
* @see CoreContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
public class InheritedConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
class InheritedConfigContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
/* all tests are in the parent class. */
|
/* all tests are in the parent class. */
|
||||||
}
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,33 +14,35 @@
|
||||||
* limitations under the License.
|
* 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.ContextConfiguration;
|
||||||
import org.springframework.util.ResourceUtils;
|
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
|
* we can specify multiple resource locations for our application context, each
|
||||||
* configured differently.
|
* configured differently.
|
||||||
*
|
*
|
||||||
* <p>{@code MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests} is also used
|
* <p>{@code MultipleResourcesContextConfigurationAppCtxTests} is also used
|
||||||
* to verify support for the {@code value} attribute alias for
|
* to verify support for the {@code value} attribute alias for
|
||||||
* {@code @ContextConfiguration}'s {@code locations} attribute.
|
* {@code @ContextConfiguration}'s {@code locations} attribute.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
* @see CoreContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration( { MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.CLASSPATH_RESOURCE_PATH,
|
@ContextConfiguration( {
|
||||||
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.LOCAL_RESOURCE_PATH,
|
MultipleResourcesContextConfigurationAppCtxTests.CLASSPATH_RESOURCE_PATH,
|
||||||
MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests.ABSOLUTE_RESOURCE_PATH })
|
MultipleResourcesContextConfigurationAppCtxTests.LOCAL_RESOURCE_PATH,
|
||||||
public class MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
MultipleResourcesContextConfigurationAppCtxTests.ABSOLUTE_RESOURCE_PATH
|
||||||
|
})
|
||||||
|
class MultipleResourcesContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
|
|
||||||
public static final String CLASSPATH_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX +
|
public static final String CLASSPATH_RESOURCE_PATH = ResourceUtils.CLASSPATH_URL_PREFIX +
|
||||||
"/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context1.xml";
|
"/org/springframework/test/context/config/MultipleResourcesContextConfigurationAppCtxTests-context1.xml";
|
||||||
public static final String LOCAL_RESOURCE_PATH = "MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context2.xml";
|
public static final String LOCAL_RESOURCE_PATH = "MultipleResourcesContextConfigurationAppCtxTests-context2.xml";
|
||||||
public static final String ABSOLUTE_RESOURCE_PATH = "/org/springframework/test/context/junit4/MultipleResourcesSpringJUnit4ClassRunnerAppCtxTests-context3.xml";
|
public static final String ABSOLUTE_RESOURCE_PATH = "/org/springframework/test/context/config/MultipleResourcesContextConfigurationAppCtxTests-context3.xml";
|
||||||
|
|
||||||
/* all tests are in the parent class. */
|
/* all tests are in the parent class. */
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,45 +14,42 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.config;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
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.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.
|
* {@link ContextConfiguration @ContextConfiguration} is optional.
|
||||||
*
|
*
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@ExtendWith(SpringExtension.class)
|
||||||
public class OptionalContextConfigurationSpringRunnerTests {
|
class OptionalContextConfigurationTests {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
String foo;
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void contextConfigurationAnnotationIsOptional() {
|
void contextConfigurationAnnotationIsOptional(@Autowired String foo) {
|
||||||
assertThat(foo).isEqualTo("foo");
|
assertThat(foo).isEqualTo("bar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration(proxyBeanMethods = false)
|
||||||
static class Config {
|
static class Config {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
String foo() {
|
String foo() {
|
||||||
return "foo";
|
return "bar";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Employee;
|
||||||
import org.springframework.beans.testfixture.beans.Pet;
|
import org.springframework.beans.testfixture.beans.Pet;
|
||||||
|
@ -25,7 +25,7 @@ import org.springframework.context.annotation.Configuration;
|
||||||
* ApplicationContext configuration class for various integration tests.
|
* ApplicationContext configuration class for various integration tests.
|
||||||
*
|
*
|
||||||
* <p>The beans defined in this configuration class map directly to the
|
* <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
|
* Consequently, the application contexts loaded from these two sources
|
||||||
* should be identical with regard to bean definitions.
|
* should be identical with regard to bean definitions.
|
||||||
*
|
*
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,21 +14,21 @@
|
||||||
* limitations under the License.
|
* 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.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
|
* we can specify an explicit, <em>relative path</em> location for our
|
||||||
* application context.
|
* application context.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see SpringJUnit4ClassRunnerAppCtxTests
|
* @see CoreContextConfigurationAppCtxTests
|
||||||
* @see AbsolutePathSpringJUnit4ClassRunnerAppCtxTests
|
* @see AbsolutePathContextConfigurationAppCtxTests
|
||||||
*/
|
*/
|
||||||
@ContextConfiguration(locations = { "SpringJUnit4ClassRunnerAppCtxTests-context.xml" })
|
@ContextConfiguration(locations = "CoreContextConfigurationAppCtxTests-context.xml", inheritLocations = false)
|
||||||
public class RelativePathSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
|
class RelativePathContextConfigurationAppCtxTests extends CoreContextConfigurationAppCtxTests {
|
||||||
/* all tests are in the parent class. */
|
/* all tests are in the parent class. */
|
||||||
}
|
}
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
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.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.testfixture.beans.Employee;
|
import org.springframework.beans.testfixture.beans.Employee;
|
||||||
import org.springframework.beans.testfixture.beans.Pet;
|
import org.springframework.beans.testfixture.beans.Pet;
|
||||||
import org.springframework.test.context.annotation.PojoAndStringConfig;
|
import org.springframework.test.context.config.PojoAndStringConfig;
|
||||||
import org.springframework.test.context.annotation.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
|
import org.springframework.test.context.config.meta.ConfigClassesAndProfilesWithCustomDefaultsMetaConfig.ProductionConfig;
|
||||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.ElementType;
|
||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* 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.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 {
|
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
|
* Run the tests in the supplied {@code testClass}, using the specified
|
||||||
* {@link Runner}, and assert the expectations of the test execution.
|
* {@link Runner}, and assert the expectations of the test execution.
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -38,32 +38,18 @@ import org.junit.runners.Suite.SuiteClasses;
|
||||||
*/
|
*/
|
||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
// Note: the following 'multi-line' layout is for enhanced code readability.
|
// Note: the following 'multi-line' layout is for enhanced code readability.
|
||||||
@SuiteClasses({//
|
@SuiteClasses({
|
||||||
StandardJUnit4FeaturesTests.class,//
|
StandardJUnit4FeaturesTests.class,
|
||||||
StandardJUnit4FeaturesSpringRunnerTests.class,//
|
StandardJUnit4FeaturesSpringRunnerTests.class,
|
||||||
SpringJUnit47ClassRunnerRuleTests.class,//
|
SpringJUnit47ClassRunnerRuleTests.class,
|
||||||
AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
|
ExpectedExceptionSpringRunnerTests.class,
|
||||||
ExpectedExceptionSpringRunnerTests.class,//
|
TimedSpringRunnerTests.class,
|
||||||
TimedSpringRunnerTests.class,//
|
RepeatedSpringRunnerTests.class,
|
||||||
RepeatedSpringRunnerTests.class,//
|
EnabledAndIgnoredSpringRunnerTests.class,
|
||||||
EnabledAndIgnoredSpringRunnerTests.class,//
|
HardCodedProfileValueSourceSpringRunnerTests.class,
|
||||||
HardCodedProfileValueSourceSpringRunnerTests.class,//
|
ParameterizedDependencyInjectionTests.class,
|
||||||
SpringJUnit4ClassRunnerAppCtxTests.class,//
|
ConcreteTransactionalJUnit4SpringContextTests.class,
|
||||||
ClassPathResourceSpringJUnit4ClassRunnerAppCtxTests.class,//
|
TimedTransactionalSpringRunnerTests.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//
|
|
||||||
})
|
})
|
||||||
public class SpringJUnit4TestSuite {
|
public class SpringJUnit4TestSuite {
|
||||||
/* this test case consists entirely of tests loaded as a suite. */
|
/* this test case consists entirely of tests loaded as a suite. */
|
||||||
|
|
|
@ -38,7 +38,7 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @see org.springframework.test.context.junit.jupiter.transaction.TimedTransactionalSpringExtensionTests
|
* @see org.springframework.test.context.junit.jupiter.transaction.TimedTransactionalSpringExtensionTests
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration("transactionalTests-context.xml")
|
@ContextConfiguration("/org/springframework/test/context/transaction/transactionalTests-context.xml")
|
||||||
@Transactional
|
@Transactional
|
||||||
public class TimedTransactionalSpringRunnerTests {
|
public class TimedTransactionalSpringRunnerTests {
|
||||||
|
|
||||||
|
|
|
@ -25,10 +25,7 @@ import org.junit.Test;
|
||||||
import org.junit.experimental.ParallelComputer;
|
import org.junit.experimental.ParallelComputer;
|
||||||
|
|
||||||
import org.springframework.core.testfixture.TestGroup;
|
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.SpringJUnit47ClassRunnerRuleTests;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests;
|
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
import org.springframework.test.context.junit4.TimedTransactionalSpringRunnerTests;
|
import org.springframework.test.context.junit4.TimedTransactionalSpringRunnerTests;
|
||||||
import org.springframework.test.context.junit4.rules.BaseAppCtxRuleTests;
|
import org.springframework.test.context.junit4.rules.BaseAppCtxRuleTests;
|
||||||
|
@ -70,12 +67,9 @@ public class SpringJUnit4ConcurrencyTests {
|
||||||
|
|
||||||
private final Class<?>[] testClasses = new Class<?>[] {
|
private final Class<?>[] testClasses = new Class<?>[] {
|
||||||
// Basics
|
// Basics
|
||||||
SpringJUnit4ClassRunnerAppCtxTests.class,
|
|
||||||
InheritedConfigSpringJUnit4ClassRunnerAppCtxTests.class,
|
|
||||||
SpringJUnit47ClassRunnerRuleTests.class,
|
SpringJUnit47ClassRunnerRuleTests.class,
|
||||||
BaseAppCtxRuleTests.class,
|
BaseAppCtxRuleTests.class,
|
||||||
// Transactional
|
// Transactional
|
||||||
MethodLevelTransactionalSpringRunnerTests.class,
|
|
||||||
TimedTransactionalSpringRunnerTests.class,
|
TimedTransactionalSpringRunnerTests.class,
|
||||||
// Web and Scopes
|
// Web and Scopes
|
||||||
BasicAnnotationConfigWacSpringRuleTests.class,
|
BasicAnnotationConfigWacSpringRuleTests.class,
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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;
|
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.ClassRule;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TestName;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
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}
|
* This class is a copy of {@code BeforeAndAfterTransactionAnnotationTests}
|
||||||
* that has been modified to use {@link SpringClassRule} and
|
* that has been modified to use JUnit 4, {@link SpringClassRule}, and
|
||||||
* {@link SpringMethodRule}.
|
* {@link SpringMethodRule}.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
@RunWith(JUnit4.class)
|
@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
|
@ClassRule
|
||||||
public static final SpringClassRule springClassRule = new SpringClassRule();
|
public static final SpringClassRule springClassRule = new SpringClassRule();
|
||||||
|
@ -40,6 +69,116 @@ public class BeforeAndAfterTransactionAnnotationSpringRuleTests extends BeforeAn
|
||||||
@Rule
|
@Rule
|
||||||
public final SpringMethodRule springMethodRule = new SpringMethodRule();
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -43,22 +43,22 @@ public class SpringFailOnTimeoutTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void nullNextStatement() {
|
public void nullNextStatement() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException()
|
||||||
new SpringFailOnTimeout(null, 1));
|
.isThrownBy(() -> new SpringFailOnTimeout(null, 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void negativeTimeout() {
|
public void negativeTimeout() {
|
||||||
assertThatIllegalArgumentException().isThrownBy(() ->
|
assertThatIllegalArgumentException()
|
||||||
new SpringFailOnTimeout(statement, -1));
|
.isThrownBy(() -> new SpringFailOnTimeout(statement, -1));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void userExceptionPropagates() throws Throwable {
|
public void userExceptionPropagates() throws Throwable {
|
||||||
willThrow(new Boom()).given(statement).evaluate();
|
willThrow(new Boom()).given(statement).evaluate();
|
||||||
|
|
||||||
assertThatExceptionOfType(Boom.class).isThrownBy(() ->
|
assertThatExceptionOfType(Boom.class)
|
||||||
new SpringFailOnTimeout(statement, 1).evaluate());
|
.isThrownBy(() -> new SpringFailOnTimeout(statement, 1).evaluate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -68,8 +68,8 @@ public class SpringFailOnTimeoutTests {
|
||||||
return null;
|
return null;
|
||||||
}).given(statement).evaluate();
|
}).given(statement).evaluate();
|
||||||
|
|
||||||
assertThatExceptionOfType(TimeoutException.class).isThrownBy(() ->
|
assertThatExceptionOfType(TimeoutException.class)
|
||||||
new SpringFailOnTimeout(statement, 1).evaluate());
|
.isThrownBy(() -> new SpringFailOnTimeout(statement, 1).evaluate());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,27 +14,25 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.testng;
|
||||||
|
|
||||||
import org.junit.Test;
|
import java.util.List;
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.runners.Parameterized.Parameters;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
import org.testng.TestNG;
|
import org.testng.TestNG;
|
||||||
|
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.TestContext;
|
import org.springframework.test.context.TestContext;
|
||||||
import org.springframework.test.context.TestExecutionListener;
|
import org.springframework.test.context.TestExecutionListener;
|
||||||
import org.springframework.test.context.TestExecutionListeners;
|
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.AfterTransaction;
|
||||||
import org.springframework.test.context.transaction.BeforeTransaction;
|
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.assertThat;
|
||||||
import static org.assertj.core.api.Assertions.fail;
|
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>'
|
* 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
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
*/
|
*/
|
||||||
@RunWith(Parameterized.class)
|
class FailingBeforeAndAfterMethodsTestNGTests {
|
||||||
public class FailingBeforeAndAfterMethodsTestNGTests {
|
|
||||||
|
|
||||||
protected final Class<?> clazz;
|
@ParameterizedTest
|
||||||
|
@MethodSource("testData")
|
||||||
protected final int expectedTestStartCount;
|
void runTestAndAssertCounters(Class<?> clazz, 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,
|
|
||||||
int expectedTestSuccessCount, int expectedFailureCount, int expectedFailedConfigurationsCount) throws Exception {
|
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();
|
TrackingTestNGTestListener listener = new TrackingTestNGTestListener();
|
||||||
TestNG testNG = new TestNG();
|
TestNG testNG = new TestNG();
|
||||||
testNG.addListener(listener);
|
testNG.addListener(listener);
|
||||||
testNG.setTestClasses(new Class<?>[] {this.clazz});
|
testNG.setTestClasses(new Class<?>[] {clazz});
|
||||||
testNG.setVerbose(0);
|
testNG.setVerbose(0);
|
||||||
testNG.run();
|
testNG.run();
|
||||||
|
|
||||||
String name = this.clazz.getSimpleName();
|
String name = clazz.getSimpleName();
|
||||||
|
|
||||||
assertThat(listener.testStartCount).as("tests started for [" + name + "] ==> ").isEqualTo(this.expectedTestStartCount);
|
assertThat(listener.testStartCount).as("tests started for [" + name + "] ==> ").isEqualTo(expectedTestStartCount);
|
||||||
assertThat(listener.testSuccessCount).as("successful tests for [" + name + "] ==> ").isEqualTo(this.expectedTestSuccessCount);
|
assertThat(listener.testSuccessCount).as("successful tests for [" + name + "] ==> ").isEqualTo(expectedTestSuccessCount);
|
||||||
assertThat(listener.testFailureCount).as("failed tests for [" + name + "] ==> ").isEqualTo(this.expectedFailureCount);
|
assertThat(listener.testFailureCount).as("failed tests for [" + name + "] ==> ").isEqualTo(expectedFailureCount);
|
||||||
assertThat(listener.failedConfigurationsCount).as("failed configurations for [" + name + "] ==> ").isEqualTo(this.expectedFailedConfigurationsCount);
|
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)
|
@TestExecutionListeners(inheritListeners = false)
|
||||||
public abstract static class BaseTestCase extends AbstractTestNGSpringContextTests {
|
abstract static class BaseTestCase extends AbstractTestNGSpringContextTests {
|
||||||
|
|
||||||
@org.testng.annotations.Test
|
@org.testng.annotations.Test
|
||||||
public void testNothing() {
|
void testNothing() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingBeforeTestClassTestExecutionListener.class)
|
||||||
public static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
|
static class AlwaysFailingBeforeTestClassTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingAfterTestClassTestExecutionListener.class)
|
||||||
public static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
|
static class AlwaysFailingAfterTestClassTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingPrepareTestInstanceTestExecutionListener.class)
|
||||||
public static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
|
static class AlwaysFailingPrepareTestInstanceTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingBeforeTestMethodTestExecutionListener.class)
|
||||||
public static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
|
static class AlwaysFailingBeforeTestMethodTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingBeforeTestExecutionTestExecutionListener.class)
|
||||||
public static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
|
static class AlwaysFailingBeforeTestExecutionTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingAfterTestExecutionTestExecutionListener.class)
|
||||||
public static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
|
static class AlwaysFailingAfterTestExecutionTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
|
@TestExecutionListeners(AlwaysFailingAfterTestMethodTestExecutionListener.class)
|
||||||
public static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
|
static class AlwaysFailingAfterTestMethodTestCase extends BaseTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
|
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
|
||||||
public static class FailingBeforeTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
static class FailingBeforeTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
||||||
|
|
||||||
@org.testng.annotations.Test
|
@org.testng.annotations.Test
|
||||||
public void testNothing() {
|
void testNothing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@BeforeTransaction
|
@BeforeTransaction
|
||||||
public void beforeTransaction() {
|
void beforeTransaction() {
|
||||||
fail("always failing beforeTransaction()");
|
fail("always failing beforeTransaction()");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
|
@ContextConfiguration("FailingBeforeAndAfterMethodsTests-context.xml")
|
||||||
public static class FailingAfterTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
static class FailingAfterTransactionTestCase extends AbstractTransactionalTestNGSpringContextTests {
|
||||||
|
|
||||||
@org.testng.annotations.Test
|
@org.testng.annotations.Test
|
||||||
public void testNothing() {
|
void testNothing() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterTransaction
|
@AfterTransaction
|
||||||
public void afterTransaction() {
|
void afterTransaction() {
|
||||||
fail("always failing afterTransaction()");
|
fail("always failing afterTransaction()");
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,27 +14,24 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import org.junit.runner.RunWith;
|
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
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;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract base class for verifying support of Spring's {@link Transactional
|
* Abstract base class for verifying support of Spring's
|
||||||
* @Transactional} annotation.
|
* {@link Transactional @Transactional} annotation.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see ClassLevelTransactionalSpringRunnerTests
|
* @see ClassLevelTransactionalSpringTests
|
||||||
* @see MethodLevelTransactionalSpringRunnerTests
|
* @see MethodLevelTransactionalSpringTests
|
||||||
* @see Transactional
|
* @see Transactional
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitConfig(locations = "transactionalTests-context.xml")
|
||||||
@ContextConfiguration("transactionalTests-context.xml")
|
abstract class AbstractTransactionalSpringTests {
|
||||||
public abstract class AbstractTransactionalSpringRunnerTests {
|
|
||||||
|
|
||||||
protected static final String BOB = "bob";
|
protected static final String BOB = "bob";
|
||||||
protected static final String JANE = "jane";
|
protected static final String JANE = "jane";
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,22 +14,21 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.After;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Rule;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.TestInfo;
|
||||||
import org.junit.rules.TestName;
|
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.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
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.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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;
|
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 BeforeTransaction @BeforeTransaction} and
|
||||||
* {@link AfterTransaction @AfterTransaction} behavior.
|
* {@link AfterTransaction @AfterTransaction} behavior.
|
||||||
*
|
*
|
||||||
|
@ -45,33 +44,31 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@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;
|
JdbcTemplate jdbcTemplate;
|
||||||
protected static int numAfterTransactionCalls = 0;
|
|
||||||
|
|
||||||
protected boolean inTransaction = false;
|
boolean inTransaction = false;
|
||||||
|
|
||||||
@Rule
|
|
||||||
public final TestName testName = new TestName();
|
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(DataSource dataSource) {
|
void setDataSource(DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeAll
|
||||||
public static void beforeClass() {
|
void beforeClass() {
|
||||||
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls = 0;
|
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls = 0;
|
||||||
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls = 0;
|
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void afterClass() {
|
void afterClass() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
|
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.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);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table after a transactional test method.").isEqualTo(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void before() {
|
void before(TestInfo testInfo) {
|
||||||
assertShouldBeInTransaction();
|
assertShouldBeInTransaction(testInfo);
|
||||||
long expected = (this.inTransaction ? 1 : 0);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table before a test method.").isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertShouldBeInTransaction() {
|
@AfterEach
|
||||||
boolean shouldBeInTransaction = !testName.getMethodName().equals("nonTransactionalMethod");
|
void after(TestInfo testInfo) {
|
||||||
if (shouldBeInTransaction) {
|
assertShouldBeInTransaction(testInfo);
|
||||||
assertThatTransaction().isActive();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
assertThatTransaction().isNotActive();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void after() {
|
|
||||||
assertShouldBeInTransaction();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void transactionalMethod1() {
|
void transactionalMethod1() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within transactionalMethod1().").isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void transactionalMethod2() {
|
void transactionalMethod2() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").isEqualTo(1);
|
||||||
|
@ -134,7 +121,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
public void nonTransactionalMethod() {
|
void nonTransactionalMethod() {
|
||||||
assertThatTransaction().isNotActive();
|
assertThatTransaction().isNotActive();
|
||||||
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").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);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,13 +14,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
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.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
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.TestExecutionListeners;
|
||||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
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.Propagation;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
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;
|
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JUnit 4 based integration test which verifies support of Spring's
|
* JUnit based integration test which verifies support of Spring's
|
||||||
* {@link Transactional @Transactional}, {@link TestExecutionListeners
|
* {@link Transactional @Transactional},
|
||||||
* @TestExecutionListeners}, and {@link ContextConfiguration
|
* {@link TestExecutionListeners @TestExecutionListeners}, and
|
||||||
* @ContextConfiguration} annotations in conjunction with the
|
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
|
||||||
* {@link SpringRunner} and the following
|
* with the following {@link TestExecutionListener TestExecutionListeners}:
|
||||||
* {@link TestExecutionListener TestExecutionListeners}:
|
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
||||||
|
@ -55,33 +55,29 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see MethodLevelTransactionalSpringRunnerTests
|
* @see MethodLevelTransactionalSpringTests
|
||||||
*/
|
*/
|
||||||
@Transactional
|
@Transactional
|
||||||
public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class ClassLevelTransactionalSpringTests extends AbstractTransactionalSpringTests {
|
||||||
|
|
||||||
protected static JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(DataSource dataSource) {
|
void setDataSource(DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@BeforeEach
|
||||||
public static void verifyFinalTestData() {
|
void verifyInitialTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void verifyInitialTestData() {
|
|
||||||
clearPersonTable(jdbcTemplate);
|
clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void modifyTestDataWithinTransaction() {
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
||||||
|
@ -91,7 +87,7 @@ public class ClassLevelTransactionalSpringRunnerTests extends AbstractTransactio
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
public void modifyTestDataWithoutTransaction() {
|
void modifyTestDataWithoutTransaction() {
|
||||||
assertThatTransaction().isNotActive();
|
assertThatTransaction().isNotActive();
|
||||||
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,19 +14,21 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.test.annotation.Rollback;
|
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 org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -43,32 +45,32 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
* @see Rollback
|
* @see Rollback
|
||||||
* @see Transactional#transactionManager
|
* @see Transactional#transactionManager
|
||||||
* @see DefaultRollbackFalseTransactionalTests
|
* @see DefaultRollbackTrueRollbackAnnotationTransactionalTests
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitConfig(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
|
||||||
@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
|
|
||||||
@Transactional("txMgr")
|
@Transactional("txMgr")
|
||||||
@Rollback(false)
|
@Rollback(false)
|
||||||
public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringTests {
|
||||||
|
|
||||||
private static JdbcTemplate jdbcTemplate;
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(DataSource dataSource) {
|
void setDataSource(DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void verifyInitialTestData() {
|
void verifyInitialTestData() {
|
||||||
clearPersonTable(jdbcTemplate);
|
clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void modifyTestDataWithinTransaction() {
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void verifyFinalTestData() {
|
void verifyFinalTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(2);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,19 +14,21 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.runner.RunWith;
|
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.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
import org.springframework.test.annotation.Rollback;
|
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 org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -43,42 +45,43 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
* @see Rollback
|
* @see Rollback
|
||||||
* @see Transactional#transactionManager
|
* @see Transactional#transactionManager
|
||||||
* @see DefaultRollbackTrueTransactionalTests
|
* @see DefaultRollbackFalseRollbackAnnotationTransactionalTests
|
||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@SpringJUnitConfig(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
|
||||||
@ContextConfiguration(classes = EmbeddedPersonDatabaseTestsConfig.class, inheritLocations = false)
|
|
||||||
@Transactional("txMgr")
|
@Transactional("txMgr")
|
||||||
@Rollback(true)
|
@Rollback(true)
|
||||||
public class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringTests {
|
||||||
|
|
||||||
private static int originalNumRows;
|
private static int originalNumRows;
|
||||||
|
|
||||||
private static JdbcTemplate jdbcTemplate;
|
JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void setDataSource(DataSource dataSource) {
|
void setDataSource(DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void verifyInitialTestData() {
|
void verifyInitialTestData() {
|
||||||
originalNumRows = clearPersonTable(jdbcTemplate);
|
originalNumRows = clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(timeout = 1000)
|
@Test
|
||||||
public void modifyTestDataWithinTransaction() {
|
@Timeout(1)
|
||||||
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void verifyFinalTestData() {
|
void verifyFinalTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,7 +14,7 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
@ -32,15 +32,15 @@ import org.springframework.transaction.PlatformTransactionManager;
|
||||||
* @since 4.2
|
* @since 4.2
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class EmbeddedPersonDatabaseTestsConfig {
|
class EmbeddedPersonDatabaseTestsConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public PlatformTransactionManager txMgr() {
|
PlatformTransactionManager txMgr() {
|
||||||
return new DataSourceTransactionManager(dataSource());
|
return new DataSourceTransactionManager(dataSource());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
DataSource dataSource() {
|
||||||
return new EmbeddedDatabaseBuilder()
|
return new EmbeddedDatabaseBuilder()
|
||||||
.generateUniqueName(true)
|
.generateUniqueName(true)
|
||||||
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")
|
.addScript("classpath:/org/springframework/test/jdbc/schema.sql")
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,13 +14,15 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.test.context.junit4;
|
package org.springframework.test.context.transaction;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.AfterClass;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
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.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
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.TestExecutionListeners;
|
||||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||||
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
|
import static org.springframework.test.transaction.TransactionAssert.assertThatTransaction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JUnit 4 based integration test which verifies support of Spring's
|
* JUnit based integration test which verifies support of Spring's
|
||||||
* {@link Transactional @Transactional}, {@link TestExecutionListeners
|
* {@link Transactional @Transactional},
|
||||||
* @TestExecutionListeners}, and {@link ContextConfiguration
|
* {@link TestExecutionListeners @TestExecutionListeners}, and
|
||||||
* @ContextConfiguration} annotations in conjunction with the
|
* {@link ContextConfiguration @ContextConfiguration} annotations in conjunction
|
||||||
* {@link SpringRunner} and the following
|
* with the following {@link TestExecutionListener TestExecutionListeners}:
|
||||||
* {@link TestExecutionListener TestExecutionListeners}:
|
|
||||||
*
|
*
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
* <li>{@link DependencyInjectionTestExecutionListener}</li>
|
||||||
|
@ -55,28 +55,24 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see ClassLevelTransactionalSpringRunnerTests
|
* @see ClassLevelTransactionalSpringTests
|
||||||
*/
|
*/
|
||||||
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
|
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
|
||||||
TransactionalTestExecutionListener.class })
|
TransactionalTestExecutionListener.class })
|
||||||
public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class MethodLevelTransactionalSpringTests extends AbstractTransactionalSpringTests {
|
||||||
|
|
||||||
protected static JdbcTemplate jdbcTemplate;
|
private JdbcTemplate jdbcTemplate;
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@Qualifier("dataSource2")
|
@Qualifier("dataSource2")
|
||||||
public void setDataSource(DataSource dataSource) {
|
void setDataSource(DataSource dataSource) {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
jdbcTemplate = new JdbcTemplate(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@BeforeEach
|
||||||
public static void verifyFinalTestData() {
|
void verifyInitialTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void verifyInitialTestData() {
|
|
||||||
clearPersonTable(jdbcTemplate);
|
clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
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
|
@Test
|
||||||
@Transactional("transactionManager2")
|
@Transactional("transactionManager2")
|
||||||
public void modifyTestDataWithinTransaction() {
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
||||||
|
@ -93,7 +89,7 @@ public class MethodLevelTransactionalSpringRunnerTests extends AbstractTransacti
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void modifyTestDataWithoutTransaction() {
|
void modifyTestDataWithoutTransaction() {
|
||||||
assertThatTransaction().isNotActive();
|
assertThatTransaction().isNotActive();
|
||||||
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, LUKE)).as("Adding luke").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, LEIA)).as("Adding leia").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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,16 +14,12 @@
|
||||||
* limitations under the License.
|
* 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 org.springframework.test.annotation.Rollback;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -38,23 +34,15 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see Rollback
|
* @see Rollback
|
||||||
*/
|
*/
|
||||||
public class RollbackOverrideDefaultRollbackFalseTransactionalTests
|
class RollbackOverrideDefaultRollbackFalseTransactionalTests
|
||||||
extends DefaultRollbackFalseRollbackAnnotationTransactionalTests {
|
extends DefaultRollbackFalseRollbackAnnotationTransactionalTests {
|
||||||
|
|
||||||
private static int originalNumRows;
|
private static int originalNumRows;
|
||||||
|
|
||||||
private static JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
@Autowired
|
|
||||||
@Override
|
@Override
|
||||||
public void setDataSource(DataSource dataSource) {
|
void verifyInitialTestData() {
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
@Override
|
|
||||||
public void verifyInitialTestData() {
|
|
||||||
originalNumRows = clearPersonTable(jdbcTemplate);
|
originalNumRows = clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
|
||||||
|
@ -63,7 +51,7 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalTests
|
||||||
@Test
|
@Test
|
||||||
@Rollback
|
@Rollback
|
||||||
@Override
|
@Override
|
||||||
public void modifyTestDataWithinTransaction() {
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
assertThat(deletePerson(jdbcTemplate, BOB)).as("Deleting bob").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void verifyFinalTestData() {
|
@Override
|
||||||
|
void verifyFinalTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(originalNumRows);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -14,16 +14,12 @@
|
||||||
* limitations under the License.
|
* 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 org.springframework.test.annotation.Rollback;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
@ -38,21 +34,12 @@ import static org.springframework.test.transaction.TransactionAssert.assertThatT
|
||||||
* @since 2.5
|
* @since 2.5
|
||||||
* @see Rollback
|
* @see Rollback
|
||||||
*/
|
*/
|
||||||
public class RollbackOverrideDefaultRollbackTrueTransactionalTests
|
class RollbackOverrideDefaultRollbackTrueTransactionalTests
|
||||||
extends DefaultRollbackTrueRollbackAnnotationTransactionalTests {
|
extends DefaultRollbackTrueRollbackAnnotationTransactionalTests {
|
||||||
|
|
||||||
private static JdbcTemplate jdbcTemplate;
|
@BeforeEach
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Autowired
|
void verifyInitialTestData() {
|
||||||
public void setDataSource(DataSource dataSource) {
|
|
||||||
jdbcTemplate = new JdbcTemplate(dataSource);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
|
||||||
@Override
|
|
||||||
public void verifyInitialTestData() {
|
|
||||||
clearPersonTable(jdbcTemplate);
|
clearPersonTable(jdbcTemplate);
|
||||||
assertThat(addPerson(jdbcTemplate, BOB)).as("Adding bob").isEqualTo(1);
|
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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the initial number of rows in the person table.").isEqualTo(1);
|
||||||
|
@ -61,15 +48,16 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalTests
|
||||||
@Test
|
@Test
|
||||||
@Rollback(false)
|
@Rollback(false)
|
||||||
@Override
|
@Override
|
||||||
public void modifyTestDataWithinTransaction() {
|
void modifyTestDataWithinTransaction() {
|
||||||
assertThatTransaction().isActive();
|
assertThatTransaction().isActive();
|
||||||
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
assertThat(addPerson(jdbcTemplate, JANE)).as("Adding jane").isEqualTo(1);
|
||||||
assertThat(addPerson(jdbcTemplate, SUE)).as("Adding sue").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);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the number of rows in the person table within a transaction.").isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterAll
|
||||||
public static void verifyFinalTestData() {
|
@Override
|
||||||
|
void verifyFinalTestData() {
|
||||||
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
|
assertThat(countRowsInPersonTable(jdbcTemplate)).as("Verifying the final number of rows in the person table after all tests.").isEqualTo(3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
|
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">
|
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:initialize-database data-source="dataSource" >
|
||||||
<jdbc:script location="classpath:/org/springframework/test/jdbc/data.sql"/>
|
<jdbc:script location="classpath:/org/springframework/test/jdbc/data.sql"/>
|
||||||
|
|
Loading…
Reference in New Issue