Support @Rollback on classes & deprecate @TxConfig

Since Spring Framework 2.5, @Rollback has been supported on test
methods, with class-level rollback settings configured via
@TransactionConfiguration; however, allowing @Rollback to be declared
on test classes with method-level declarations overriding class-level
declarations would prove more intuitive than having to declare both
@TransactionConfiguration and @Rollback. Furthermore, the
transactionManager flag in @TransactionConfiguration was made
superfluous many years ago with the introduction of support for a
qualifier in @Transactional.

This commit enables @Rollback to be declared at the class level for
default rollback semantics within test class hierarchies and deprecates
@TransactionConfiguration in favor of @Rollback and @Transactional
qualifiers.

Issue: SPR-13276, SPR-13277
This commit is contained in:
Sam Brannen
2015-07-25 18:22:26 +02:00
parent efd7f9bf72
commit 3f8b51283e
22 changed files with 713 additions and 196 deletions

View File

@@ -0,0 +1,110 @@
/*
* Copyright 2002-2015 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
*
* http://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.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Integration test which verifies proper transactional behavior when the
* default rollback flag is set to {@code false} via {@link Rollback @Rollback}.
*
* <p>Also tests configuration of the transaction manager qualifier configured
* via {@link Transactional @Transactional}.
*
* @author Sam Brannen
* @since 4.2
* @see Rollback
* @see Transactional#transactionManager
* @see DefaultRollbackFalseTransactionalTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(inheritLocations = false)
@Transactional("txMgr")
@Rollback(false)
public class DefaultRollbackFalseRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
private static JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(jdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
countRowsInPersonTable(jdbcTemplate));
}
@Configuration
static class Config {
@Bean
public PlatformTransactionManager txMgr() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
.generateUniqueName(true)//
.addScript("classpath:/org/springframework/test/context/junit4/person-schema.sql") //
.build();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -23,7 +23,6 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
@@ -33,33 +32,27 @@ import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* <p>
* JUnit 4 based integration test which verifies proper transactional behavior when the
* {@link TransactionConfiguration#defaultRollback() defaultRollback} attribute
* of the {@link TransactionConfiguration} annotation is set to <strong>{@code false}</strong>.
* Also tests configuration of the
* <p>Also tests configuration of the
* {@link TransactionConfiguration#transactionManager() transaction manager name}.
* </p>
*
* @author Sam Brannen
* @since 2.5
* @see TransactionConfiguration
* @see DefaultRollbackFalseRollbackAnnotationTransactionalTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TransactionConfiguration(transactionManager = "txMgr", defaultRollback = false)
@Transactional
public class DefaultRollbackFalseTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
@SuppressWarnings("deprecation")
public class DefaultRollbackFalseTransactionalTests extends AbstractTransactionalSpringRunnerTests {
protected static JdbcTemplate jdbcTemplate;
private static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
@@ -78,6 +71,12 @@ public class DefaultRollbackFalseTransactionalSpringRunnerTests extends Abstract
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 2,
countRowsInPersonTable(jdbcTemplate));
}
public static class DatabaseSetup {

View File

@@ -0,0 +1,111 @@
/*
* Copyright 2002-2015 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
*
* http://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.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Integration test which verifies proper transactional behavior when the default
* rollback flag is explicitly set to {@code true} via {@link Rollback @Rollback}.
*
* <p>Also tests configuration of the transaction manager qualifier configured
* via {@link Transactional @Transactional}.
*
* @author Sam Brannen
* @since 4.2
* @see Rollback
* @see Transactional#transactionManager
* @see DefaultRollbackTrueTransactionalTests
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(inheritLocations = false)
@Transactional("txMgr")
@Rollback(true)
public class DefaultRollbackTrueRollbackAnnotationTransactionalTests extends AbstractTransactionalSpringRunnerTests {
private static int originalNumRows;
private static JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test(timeout = 1000)
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 3,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
@Configuration
static class Config {
@Bean
public PlatformTransactionManager txMgr() {
return new DataSourceTransactionManager(dataSource());
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()//
.generateUniqueName(true)//
.addScript("classpath:/org/springframework/test/context/junit4/person-schema.sql") //
.build();
}
}
}

View File

@@ -23,7 +23,6 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.transaction.TransactionConfiguration;
@@ -43,20 +42,16 @@ import static org.springframework.test.transaction.TransactionTestUtils.*;
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@Transactional
@TransactionConfiguration(defaultRollback = true)
public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractTransactionalSpringRunnerTests {
@SuppressWarnings("deprecation")
public class DefaultRollbackTrueTransactionalTests extends AbstractTransactionalSpringRunnerTests {
protected static int originalNumRows;
private static int originalNumRows;
protected static JdbcTemplate jdbcTemplate;
private static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
@Before
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
@@ -66,7 +61,6 @@ public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractT
}
@Test(timeout = 1000)
@Transactional
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
@@ -75,6 +69,12 @@ public class DefaultRollbackTrueTransactionalSpringRunnerTests extends AbstractT
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
public static class DatabaseSetup {

View File

@@ -0,0 +1,82 @@
/*
* Copyright 2002-2015 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
*
* http://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.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* 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;
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new JdbcTemplate(dataSource);
}
@Before
@Override
public void verifyInitialTestData() {
originalNumRows = clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test
@Rollback
@Override
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Deleting bob", 1, deletePerson(jdbcTemplate, BOB));
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 2,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,7 @@ import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Extension of {@link DefaultRollbackFalseTransactionalSpringRunnerTests} which
* Extension of {@link DefaultRollbackFalseTransactionalTests} which
* tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
@@ -40,20 +40,14 @@ import static org.springframework.test.transaction.TransactionTestUtils.*;
* @see Rollback
*/
@ContextConfiguration
public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests extends
DefaultRollbackFalseTransactionalSpringRunnerTests {
public class RollbackOverrideDefaultRollbackFalseTransactionalTests extends
DefaultRollbackFalseTransactionalTests {
protected static int originalNumRows;
private static int originalNumRows;
protected static JdbcTemplate jdbcTemplate;
private static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
@Before
@Override
public void verifyInitialTestData() {
@@ -75,6 +69,12 @@ public class RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", originalNumRows,
countRowsInPersonTable(jdbcTemplate));
}
public static class DatabaseSetup {

View File

@@ -0,0 +1,80 @@
/*
* Copyright 2002-2015 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
*
* http://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.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* 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);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
assertEquals("Verifying the initial number of rows in the person table.", 1,
countRowsInPersonTable(jdbcTemplate));
}
@Test
@Rollback(false)
@Override
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
assertEquals("Adding sue", 1, addPerson(jdbcTemplate, SUE));
assertEquals("Verifying the number of rows in the person table within a transaction.", 3,
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
countRowsInPersonTable(jdbcTemplate));
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -26,13 +26,12 @@ import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.Assert.*;
import static org.springframework.test.transaction.TransactionTestUtils.*;
/**
* Extension of {@link DefaultRollbackTrueTransactionalSpringRunnerTests} which
* Extension of {@link DefaultRollbackTrueTransactionalTests} which
* tests method-level <em>rollback override</em> behavior via the
* {@link Rollback @Rollback} annotation.
*
@@ -41,20 +40,14 @@ import static org.springframework.test.transaction.TransactionTestUtils.*;
* @see Rollback
*/
@ContextConfiguration
public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests extends
DefaultRollbackTrueTransactionalSpringRunnerTests {
public class RollbackOverrideDefaultRollbackTrueTransactionalTests extends
DefaultRollbackTrueTransactionalTests {
protected static JdbcTemplate jdbcTemplate;
private static JdbcTemplate jdbcTemplate;
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
countRowsInPersonTable(jdbcTemplate));
}
@Override
@Before
@Override
public void verifyInitialTestData() {
clearPersonTable(jdbcTemplate);
assertEquals("Adding bob", 1, addPerson(jdbcTemplate, BOB));
@@ -62,10 +55,9 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests e
countRowsInPersonTable(jdbcTemplate));
}
@Override
@Test
@Transactional
@Rollback(false)
@Override
public void modifyTestDataWithinTransaction() {
assertInTransaction(true);
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
@@ -74,6 +66,12 @@ public class RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests e
countRowsInPersonTable(jdbcTemplate));
}
@AfterClass
public static void verifyFinalTestData() {
assertEquals("Verifying the final number of rows in the person table after all tests.", 3,
countRowsInPersonTable(jdbcTemplate));
}
public static class DatabaseSetup {

View File

@@ -55,7 +55,7 @@ import org.springframework.test.context.transaction.programmatic.ProgrammaticTxM
*
* <p>Note that tests included in this suite will be executed at least twice if
* run from an automated build process, test runner, etc. that is not configured
* to exclude tests based on a &quot;*TestSuite.class&quot; pattern match.
* to exclude tests based on a {@code "*TestSuite.class"} pattern match.
*
* @author Sam Brannen
* @since 2.5
@@ -104,10 +104,10 @@ StandardJUnit4FeaturesTests.class,//
ConcreteTransactionalJUnit4SpringContextTests.class,//
ClassLevelTransactionalSpringRunnerTests.class,//
MethodLevelTransactionalSpringRunnerTests.class,//
DefaultRollbackTrueTransactionalSpringRunnerTests.class,//
DefaultRollbackFalseTransactionalSpringRunnerTests.class,//
RollbackOverrideDefaultRollbackTrueTransactionalSpringRunnerTests.class,//
RollbackOverrideDefaultRollbackFalseTransactionalSpringRunnerTests.class,//
DefaultRollbackTrueTransactionalTests.class,//
DefaultRollbackFalseTransactionalTests.class,//
RollbackOverrideDefaultRollbackTrueTransactionalTests.class,//
RollbackOverrideDefaultRollbackFalseTransactionalTests.class,//
BeforeAndAfterTransactionAnnotationTests.class,//
TimedTransactionalSpringRunnerTests.class,//
ProgrammaticTxMgmtTests.class,//

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,9 +20,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.junit.After;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.BDDMockito;
import org.springframework.core.annotation.AliasFor;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.TestContext;
import org.springframework.transaction.PlatformTransactionManager;
@@ -31,6 +35,8 @@ import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.SimpleTransactionStatus;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
import static org.springframework.transaction.annotation.Propagation.*;
@@ -41,6 +47,7 @@ import static org.springframework.transaction.annotation.Propagation.*;
* @author Sam Brannen
* @since 4.0
*/
@SuppressWarnings("deprecation")
public class TransactionalTestExecutionListenerTests {
private final PlatformTransactionManager tm = mock(PlatformTransactionManager.class);
@@ -54,6 +61,9 @@ public class TransactionalTestExecutionListenerTests {
private final TestContext testContext = mock(TestContext.class);
@Rule
public ExpectedException exception = ExpectedException.none();
private void assertBeforeTestMethod(Class<? extends Invocable> clazz) throws Exception {
assertBeforeTestMethodWithTransactionalTestMethod(clazz);
@@ -228,24 +238,86 @@ public class TransactionalTestExecutionListenerTests {
"overriddenTxMgr", true);
}
@Test
public void retrieveConfigurationAttributesWithEmptyTransactionalAnnotation() throws Exception {
assertTransactionConfigurationAttributes(EmptyTransactionalTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationWithExplicitQualifier() throws Exception {
// The test class configures "tm" as the qualifier via @Transactional;
// however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalWithExplicitQualifierTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationViaMetaAnnotation() throws Exception {
// The test class configures "metaTxMgr" as the qualifier via @Transactional;
// however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalViaMetaAnnotationTestCase.class, "", true);
}
@Test
public void retrieveConfigurationAttributesFromTransactionalAnnotationViaMetaAnnotationWithExplicitQualifier()
throws Exception {
// The test class configures "overriddenTxMgr" as the qualifier via
// @Transactional; however, retrieveConfigurationAttributes() only supports
// @TransactionConfiguration. So we actually expect "" as the qualifier here,
// relying on beforeTestMethod() to properly obtain the actual qualifier via the
// TransactionAttribute.
assertTransactionConfigurationAttributes(TransactionalViaMetaAnnotationWithExplicitQualifierTestCase.class, "",
true);
}
@Test
public void isRollbackWithMissingRollback() throws Exception {
assertIsRollback(MissingRollbackTestCase.class, true);
}
@Test
public void isRollbackWithEmptyRollback() throws Exception {
assertIsRollback(EmptyRollbackTestCase.class, true);
public void isRollbackWithEmptyMethodLevelRollback() throws Exception {
assertIsRollback(EmptyMethodLevelRollbackTestCase.class, true);
}
@Test
public void isRollbackWithExplicitValue() throws Exception {
assertIsRollback(RollbackWithExplicitValueTestCase.class, false);
public void isRollbackWithMethodLevelRollbackWithExplicitValue() throws Exception {
assertIsRollback(MethodLevelRollbackWithExplicitValueTestCase.class, false);
}
@Test
public void isRollbackViaMetaAnnotation() throws Exception {
assertIsRollback(RollbackViaMetaAnnotationTestCase.class, false);
public void isRollbackWithMethodLevelRollbackViaMetaAnnotation() throws Exception {
assertIsRollback(MethodLevelRollbackViaMetaAnnotationTestCase.class, false);
}
@Test
public void isRollbackWithEmptyClassLevelRollback() throws Exception {
assertIsRollback(EmptyClassLevelRollbackTestCase.class, true);
}
@Test
public void isRollbackWithClassLevelRollbackWithExplicitValue() throws Exception {
assertIsRollback(ClassLevelRollbackWithExplicitValueTestCase.class, false);
}
@Test
public void isRollbackWithClassLevelRollbackViaMetaAnnotation() throws Exception {
assertIsRollback(ClassLevelRollbackViaMetaAnnotationTestCase.class, false);
}
@Test
public void isRollbackWithRollbackAndTransactionConfigurationDeclaredAtClassLevel() throws Exception {
Class<?> clazz = ClassLevelRollbackAndTransactionConfigurationTestCase.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
exception.expect(IllegalStateException.class);
exception.expectMessage(containsString("annotated with both @Rollback and @TransactionConfiguration, but only one is permitted"));
listener.isRollback(testContext);
}
@@ -260,6 +332,9 @@ public class TransactionalTestExecutionListenerTests {
@Retention(RetentionPolicy.RUNTIME)
private static @interface MetaTxWithOverride {
@AliasFor(annotation = Transactional.class, attribute = "value")
String transactionManager() default "";
Propagation propagation() default REQUIRED;
}
@@ -467,31 +542,76 @@ public class TransactionalTestExecutionListenerTests {
static class TransactionConfigurationViaMetaAnnotationWithOverrideTestCase {
}
@Transactional
static class EmptyTransactionalTestCase {
}
@Transactional(transactionManager = "tm")
static class TransactionalWithExplicitQualifierTestCase {
}
@MetaTransactional
static class TransactionalViaMetaAnnotationTestCase {
}
@MetaTxWithOverride(transactionManager = "tm")
static class TransactionalViaMetaAnnotationWithExplicitQualifierTestCase {
}
static class MissingRollbackTestCase {
public void test() {
}
}
static class EmptyRollbackTestCase {
static class EmptyMethodLevelRollbackTestCase {
@Rollback
public void test() {
}
}
static class RollbackWithExplicitValueTestCase {
static class MethodLevelRollbackWithExplicitValueTestCase {
@Rollback(false)
public void test() {
}
}
static class RollbackViaMetaAnnotationTestCase {
static class MethodLevelRollbackViaMetaAnnotationTestCase {
@Commit
public void test() {
}
}
@Rollback
@TransactionConfiguration
static class ClassLevelRollbackAndTransactionConfigurationTestCase {
public void test() {
}
}
@Rollback
static class EmptyClassLevelRollbackTestCase {
public void test() {
}
}
@Rollback(false)
static class ClassLevelRollbackWithExplicitValueTestCase {
public void test() {
}
}
@Commit
static class ClassLevelRollbackViaMetaAnnotationTestCase {
public void test() {
}
}
}