Skip to content

Commit a2bfe86

Browse files
committed
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
1 parent 31e0386 commit a2bfe86

File tree

8 files changed

+28
-24
lines changed

8 files changed

+28
-24
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import static java.lang.annotation.RetentionPolicy.*;
2525

2626
/**
27-
* <p>Test annotation to indicate that the annotated {@code public void} method
27+
* <p>Test annotation to indicate that the annotated {@code void} method
2828
* should be executed <em>after</em> a transaction is ended for a test method
2929
* configured to run within a transaction via the {@code @Transactional} annotation.
3030
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
import static java.lang.annotation.RetentionPolicy.*;
2525

2626
/**
27-
* <p>Test annotation to indicate that the annotated {@code public void} method
27+
* <p>Test annotation to indicate that the annotated {@code void} method
2828
* should be executed <em>before</em> a transaction is started for a test method
2929
* configured to run within a transaction via the {@code @Transactional} annotation.
3030
*

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -246,6 +246,7 @@ protected void runBeforeTransactionMethods(TestContext testContext) throws Excep
246246
if (logger.isDebugEnabled()) {
247247
logger.debug("Executing @BeforeTransaction method [" + method + "] for test context " + testContext);
248248
}
249+
ReflectionUtils.makeAccessible(method);
249250
method.invoke(testContext.getTestInstance());
250251
}
251252
}
@@ -273,6 +274,7 @@ protected void runAfterTransactionMethods(TestContext testContext) throws Except
273274
if (logger.isDebugEnabled()) {
274275
logger.debug("Executing @AfterTransaction method [" + method + "] for test context " + testContext);
275276
}
277+
ReflectionUtils.makeAccessible(method);
276278
method.invoke(testContext.getTestInstance());
277279
}
278280
catch (InvocationTargetException ex) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import org.springframework.jdbc.core.JdbcTemplate;
3131
import org.springframework.test.context.transaction.AfterTransaction;
3232
import org.springframework.test.context.transaction.BeforeTransaction;
33+
import org.springframework.transaction.annotation.Propagation;
3334
import org.springframework.transaction.annotation.Transactional;
3435

3536
import static org.junit.Assert.*;
@@ -43,6 +44,7 @@
4344
* @author Sam Brannen
4445
* @since 2.5
4546
*/
47+
@Transactional
4648
public class BeforeAndAfterTransactionAnnotationTests extends AbstractTransactionalSpringRunnerTests {
4749

4850
protected static JdbcTemplate jdbcTemplate;
@@ -79,7 +81,7 @@ public static void afterClass() {
7981
}
8082

8183
@BeforeTransaction
82-
public void beforeTransaction() {
84+
void beforeTransaction() {
8385
assertInTransaction(false);
8486
this.inTransaction = true;
8587
BeforeAndAfterTransactionAnnotationTests.numBeforeTransactionCalls++;
@@ -88,7 +90,7 @@ public void beforeTransaction() {
8890
}
8991

9092
@AfterTransaction
91-
public void afterTransaction() {
93+
void afterTransaction() {
9294
assertInTransaction(false);
9395
this.inTransaction = false;
9496
BeforeAndAfterTransactionAnnotationTests.numAfterTransactionCalls++;
@@ -115,7 +117,6 @@ public void after() {
115117
}
116118

117119
@Test
118-
@Transactional
119120
public void transactionalMethod1() {
120121
assertInTransaction(true);
121122
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
@@ -124,7 +125,6 @@ public void transactionalMethod1() {
124125
}
125126

126127
@Test
127-
@Transactional
128128
public void transactionalMethod2() {
129129
assertInTransaction(true);
130130
assertEquals("Adding jane", 1, addPerson(jdbcTemplate, JANE));
@@ -134,6 +134,7 @@ public void transactionalMethod2() {
134134
}
135135

136136
@Test
137+
@Transactional(propagation = Propagation.NOT_SUPPORTED)
137138
public void nonTransactionalMethod() {
138139
assertInTransaction(false);
139140
assertEquals("Adding luke", 1, addPerson(jdbcTemplate, LUKE));

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -120,7 +120,7 @@ public void autowiringFromConfigClass() {
120120
}
121121

122122
@BeforeTransaction
123-
public void beforeTransaction() {
123+
void beforeTransaction() {
124124
assertNumRowsInPersonTable(1, "before a transactional test method");
125125
assertAddPerson(YODA);
126126
}
@@ -152,7 +152,7 @@ public void tearDown() throws Exception {
152152
}
153153

154154
@AfterTransaction
155-
public void afterTransaction() {
155+
void afterTransaction() {
156156
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
157157
assertNumRowsInPersonTable(1, "after a transactional test method");
158158
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -190,7 +190,7 @@ public void verifyResourceAnnotationInjectedMethods() {
190190
}
191191

192192
@BeforeTransaction
193-
public void beforeTransaction() {
193+
void beforeTransaction() {
194194
assertNumRowsInPersonTable(1, "before a transactional test method");
195195
assertAddPerson(YODA);
196196
}
@@ -222,7 +222,7 @@ public void tearDown() throws Exception {
222222
}
223223

224224
@AfterTransaction
225-
public void afterTransaction() {
225+
void afterTransaction() {
226226
assertEquals(deletePerson(YODA), 1, "Deleting yoda");
227227
assertNumRowsInPersonTable(1, "after a transactional test method");
228228
}

src/asciidoc/testing.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ method, potentially overriding class-level `@Rollback` or `@Commit` semantics.
853853

854854
+
855855

856-
Indicates that the annotated `public void` method should be executed __before__ a
856+
Indicates that the annotated `void` method should be executed __before__ a
857857
transaction is started for test methods configured to run within a transaction via the
858858
`@Transactional` annotation.
859859

@@ -863,7 +863,7 @@ transaction is started for test methods configured to run within a transaction v
863863
[subs="verbatim,quotes"]
864864
----
865865
**@BeforeTransaction**
866-
public void beforeTransaction() {
866+
void beforeTransaction() {
867867
// logic to be executed before a transaction is started
868868
}
869869
----
@@ -872,7 +872,7 @@ transaction is started for test methods configured to run within a transaction v
872872

873873
+
874874

875-
Indicates that the annotated `public void` method should be executed __after__ a
875+
Indicates that the annotated `void` method should be executed __after__ a
876876
transaction has ended for test methods configured to run within a transaction via the
877877
`@Transactional` annotation.
878878

@@ -882,7 +882,7 @@ transaction has ended for test methods configured to run within a transaction vi
882882
[subs="verbatim,quotes"]
883883
----
884884
**@AfterTransaction**
885-
public void afterTransaction() {
885+
void afterTransaction() {
886886
// logic to be executed after a transaction has ended
887887
}
888888
----
@@ -3197,8 +3197,8 @@ but outside the transactional context -- for example, to verify the initial data
31973197
prior to execution of your test or to verify expected transactional commit behavior after
31983198
test execution (if the test was configured not to roll back the transaction).
31993199
`TransactionalTestExecutionListener` supports the `@BeforeTransaction` and
3200-
`@AfterTransaction` annotations exactly for such scenarios. Simply annotate any `public
3201-
void` method in your test class with one of these annotations, and the
3200+
`@AfterTransaction` annotations exactly for such scenarios. Simply annotate any `void`
3201+
method in your test class with one of these annotations, and the
32023202
`TransactionalTestExecutionListener` ensures that your __before transaction method__ or
32033203
__after transaction method__ is executed at the appropriate time.
32043204

@@ -3244,7 +3244,7 @@ declarative SQL script execution with default transaction rollback semantics.
32443244
public class FictitiousTransactionalTest {
32453245
32463246
**@BeforeTransaction**
3247-
public void verifyInitialDatabaseState() {
3247+
void verifyInitialDatabaseState() {
32483248
// logic to verify the initial state before a transaction is started
32493249
}
32503250
@@ -3266,7 +3266,7 @@ declarative SQL script execution with default transaction rollback semantics.
32663266
}
32673267
32683268
**@AfterTransaction**
3269-
public void verifyFinalDatabaseState() {
3269+
void verifyFinalDatabaseState() {
32703270
// logic to verify the final state after transaction has rolled back
32713271
}
32723272

src/asciidoc/whats-new.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ Spring 4.3 also improves the caching abstraction as follows:
677677

678678
* The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.
679679
* New `SpringRunner` __alias__ for the `SpringJUnit4ClassRunner`.
680+
* `@BeforeTransaction` and `@AfterTransaction` methods are no longer required to be `public`.
680681
* Server-side Spring MVC Test supports expectations on response headers with multiple values.
681682
* Server-side Spring MVC Test parses form data request content and populates request parameters.
682683
* Client-side REST test support allows indicating how many times a request is expected and

0 commit comments

Comments
 (0)