Browse Source

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
pull/982/head
Sam Brannen 10 years ago
parent
commit
a2bfe86630
  1. 4
      spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java
  2. 4
      spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java
  3. 4
      spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java
  4. 11
      spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java
  5. 6
      spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java
  6. 6
      spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java
  7. 16
      src/asciidoc/testing.adoc
  8. 1
      src/asciidoc/whats-new.adoc

4
spring-test/src/main/java/org/springframework/test/context/transaction/AfterTransaction.java

@ -1,5 +1,5 @@ @@ -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.*; @@ -24,7 +24,7 @@ import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* <p>Test annotation to indicate that the annotated {@code public void} method
* <p>Test annotation to indicate that the annotated {@code void} method
* should be executed <em>after</em> a transaction is ended for a test method
* configured to run within a transaction via the {@code @Transactional} annotation.
*

4
spring-test/src/main/java/org/springframework/test/context/transaction/BeforeTransaction.java

@ -1,5 +1,5 @@ @@ -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.*; @@ -24,7 +24,7 @@ import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
/**
* <p>Test annotation to indicate that the annotated {@code public void} method
* <p>Test annotation to indicate that the annotated {@code void} method
* should be executed <em>before</em> a transaction is started for a test method
* configured to run within a transaction via the {@code @Transactional} annotation.
*

4
spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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) {

11
spring-test/src/test/java/org/springframework/test/context/junit4/BeforeAndAfterTransactionAnnotationTests.java

@ -1,5 +1,5 @@ @@ -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; @@ -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.*; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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));

6
spring-test/src/test/java/org/springframework/test/context/testng/AnnotationConfigTransactionalTestNGSpringContextTests.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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");
}

6
spring-test/src/test/java/org/springframework/test/context/testng/ConcreteTransactionalTestNGSpringContextTests.java

@ -1,5 +1,5 @@ @@ -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 @@ -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 @@ -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");
}

16
src/asciidoc/testing.adoc

@ -853,7 +853,7 @@ method, potentially overriding class-level `@Rollback` or `@Commit` semantics. @@ -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 @@ -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 @@ -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 @@ -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 @@ -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. @@ -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. @@ -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
}

1
src/asciidoc/whats-new.adoc

@ -677,6 +677,7 @@ Spring 4.3 also improves the caching abstraction as follows: @@ -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

Loading…
Cancel
Save