From a2bfe86630a9407d0f33236f7f8d6edcc6cb12dc Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 28 Feb 2016 23:36:21 +0100 Subject: [PATCH] Support @[Before|After]Transaction on non-public methods In order to align with the relaxed programming models of TestNG and the upcoming JUnit 5 (with regard to method visibility), this commit removes the requirement that @BeforeTransaction and @AfterTransaction methods must be 'public'. Issue: SPR-13997 --- .../context/transaction/AfterTransaction.java | 4 ++-- .../context/transaction/BeforeTransaction.java | 4 ++-- .../TransactionalTestExecutionListener.java | 4 +++- ...BeforeAndAfterTransactionAnnotationTests.java | 11 ++++++----- ...figTransactionalTestNGSpringContextTests.java | 6 +++--- ...eteTransactionalTestNGSpringContextTests.java | 6 +++--- src/asciidoc/testing.adoc | 16 ++++++++-------- src/asciidoc/whats-new.adoc | 1 + 8 files changed, 28 insertions(+), 24 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java b/spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java index ea21cc7fb70..3aa32129e30 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -24,7 +24,7 @@ import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; /** - *

Test annotation to indicate that the annotated {@code public void} method + *

Test annotation to indicate that the annotated {@code void} method * should be executed after a transaction is ended for a test method * configured to run within a transaction via the {@code @Transactional} annotation. * diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java b/spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java index 05106390cab..b81801c5a59 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2016 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. @@ -24,7 +24,7 @@ import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; /** - *

Test annotation to indicate that the annotated {@code public void} method + *

Test annotation to indicate that the annotated {@code void} method * should be executed before a transaction is started for a test method * configured to run within a transaction via the {@code @Transactional} annotation. * diff --git a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java index 6ae8d4bd9a5..9e0c54b8b00 100644 --- a/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -246,6 +246,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis if (logger.isDebugEnabled()) { logger.debug("Executing @BeforeTransaction method [" + method + "] for test context " + testContext); } + ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } } @@ -273,6 +274,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis if (logger.isDebugEnabled()) { logger.debug("Executing @AfterTransaction method [" + method + "] for test context " + testContext); } + ReflectionUtils.makeAccessible(method); method.invoke(testContext.getTestInstance()); } catch (InvocationTargetException ex) { diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java index 23440f7df11..4596e6cf8ca 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2016 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. @@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.transaction.AfterTransaction; import org.springframework.test.context.transaction.BeforeTransaction; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import static org.junit.Assert.*; @@ -43,6 +44,7 @@ import static org.springframework.test.transaction.TransactionTestUtils.*; * @author Sam Brannen * @since 2.5 */ +@Transactional public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests { protected static JdbcTemplate jdbcTemplate; @@ -79,7 +81,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio } @BeforeTransaction - public void beforeTransaction() { + void beforeTransaction() { assertInTransaction(false); this.inTransaction = true; BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls++; @@ -88,7 +90,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio } @AfterTransaction - public void afterTransaction() { + void afterTransaction() { assertInTransaction(false); this.inTransaction = false; BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls++; @@ -115,7 +117,6 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio } @Test - @Transactional public void transactionalMethod1() { assertInTransaction(true); assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE)); @@ -124,7 +125,6 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio } @Test - @Transactional public void transactionalMethod2() { assertInTransaction(true); assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE)); @@ -134,6 +134,7 @@ public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactio } @Test + @Transactional(propagation = Propagation.NOT_SUPPORTED) public void nonTransactionalMethod() { assertInTransaction(false); assertEquals("Adding luke", 1, addPerson(jdbcTemplate, LUKE)); diff --git a/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java b/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java index a7c9f9745d7..369e16fe081 100644 --- a/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -120,7 +120,7 @@ public class AnnotationConfigTransactionalTestNGSpringContextTests extends } @BeforeTransaction - public void beforeTransaction() { + void beforeTransaction() { assertNumRowsInPersonTable(1, "before a transactional test method"); assertAddPerson(YODA); } @@ -152,7 +152,7 @@ public class AnnotationConfigTransactionalTestNGSpringContextTests extends } @AfterTransaction - public void afterTransaction() { + void afterTransaction() { assertEquals(deletePerson(YODA), 1, "Deleting yoda"); assertNumRowsInPersonTable(1, "after a transactional test method"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java b/spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java index 2acc370ec89..50f2a2ce9bf 100644 --- a/spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2016 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. @@ -190,7 +190,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans } @BeforeTransaction - public void beforeTransaction() { + void beforeTransaction() { assertNumRowsInPersonTable(1, "before a transactional test method"); assertAddPerson(YODA); } @@ -222,7 +222,7 @@ public class ConcreteTransactionalTestNGSpringContextTests extends AbstractTrans } @AfterTransaction - public void afterTransaction() { + void afterTransaction() { assertEquals(deletePerson(YODA), 1, "Deleting yoda"); assertNumRowsInPersonTable(1, "after a transactional test method"); } diff --git a/src/asciidoc/testing.adoc b/src/asciidoc/testing.adoc index 1a61118f168..dbf6cad3c0c 100644 --- a/src/asciidoc/testing.adoc +++ b/src/asciidoc/testing.adoc @@ -853,7 +853,7 @@ method, potentially overriding class-level `@Rollback` or `@Commit` semantics. + -Indicates that the annotated `public void` method should be executed __before__ a +Indicates that the annotated `void` method should be executed __before__ a transaction is started for test methods configured to run within a transaction via the `@Transactional` annotation. @@ -863,7 +863,7 @@ transaction is started for test methods configured to run within a transaction v [subs="verbatim,quotes"] ---- **@BeforeTransaction** - public void beforeTransaction() { + void beforeTransaction() { // logic to be executed before a transaction is started } ---- @@ -872,7 +872,7 @@ transaction is started for test methods configured to run within a transaction v + -Indicates that the annotated `public void` method should be executed __after__ a +Indicates that the annotated `void` method should be executed __after__ a transaction has ended for test methods configured to run within a transaction via the `@Transactional` annotation. @@ -882,7 +882,7 @@ transaction has ended for test methods configured to run within a transaction vi [subs="verbatim,quotes"] ---- **@AfterTransaction** - public void afterTransaction() { + void afterTransaction() { // logic to be executed after a transaction has ended } ---- @@ -3197,8 +3197,8 @@ but outside the transactional context -- for example, to verify the initial data prior to execution of your test or to verify expected transactional commit behavior after test execution (if the test was configured not to roll back the transaction). `TransactionalTestExecutionListener` supports the `@BeforeTransaction` and -`@AfterTransaction` annotations exactly for such scenarios. Simply annotate any `public -void` method in your test class with one of these annotations, and the +`@AfterTransaction` annotations exactly for such scenarios. Simply annotate any `void` +method in your test class with one of these annotations, and the `TransactionalTestExecutionListener` ensures that your __before transaction method__ or __after transaction method__ is executed at the appropriate time. @@ -3244,7 +3244,7 @@ declarative SQL script execution with default transaction rollback semantics. public class FictitiousTransactionalTest { **@BeforeTransaction** - public void verifyInitialDatabaseState() { + void verifyInitialDatabaseState() { // logic to verify the initial state before a transaction is started } @@ -3266,7 +3266,7 @@ declarative SQL script execution with default transaction rollback semantics. } **@AfterTransaction** - public void verifyFinalDatabaseState() { + void verifyFinalDatabaseState() { // logic to verify the final state after transaction has rolled back } diff --git a/src/asciidoc/whats-new.adoc b/src/asciidoc/whats-new.adoc index a7adee3a0bb..3055500e261 100644 --- a/src/asciidoc/whats-new.adoc +++ b/src/asciidoc/whats-new.adoc @@ -677,6 +677,7 @@ Spring 4.3 also improves the caching abstraction as follows: * The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher. * New `SpringRunner` __alias__ for the `SpringJUnit4ClassRunner`. +* `@BeforeTransaction` and `@AfterTransaction` methods are no longer required to be `public`. * Server-side Spring MVC Test supports expectations on response headers with multiple values. * Server-side Spring MVC Test parses form data request content and populates request parameters. * Client-side REST test support allows indicating how many times a request is expected and