55 lines
1.7 KiB
Plaintext
55 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
In tests configured with Spring's `@Transactional` annotation, methods annotated with `@BeforeTransaction` or `@AfterTransaction` must be void and have no arguments.
|
|
These methods are executed before or after a transaction, respectively.
|
|
Deviating from this contract by having a non-void return type or accepting arguments will cause Spring to throw a runtime error.
|
|
|
|
== How to fix it
|
|
|
|
Ensure that methods annotated with `@BeforeTransaction` or `@AfterTransaction` have a void return type and do not accept any arguments.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@Transactional
|
|
public class TransactionalTest {
|
|
|
|
@BeforeTransaction
|
|
public String setupTransaction(int x) { // non-compliant, method should be void and have no argument
|
|
// Setup logic
|
|
}
|
|
|
|
@AfterTransaction
|
|
public int cleanupTransaction(int x) { // non-compliant, method should be void and have no argument
|
|
// Cleanup logic
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@Transactional
|
|
public class TransactionalTest {
|
|
|
|
@BeforeTransaction
|
|
public void setupTransaction() {
|
|
// Setup logic
|
|
}
|
|
|
|
@AfterTransaction
|
|
public void cleanupTransaction() {
|
|
// Cleanup logic
|
|
}
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
=== Documentation
|
|
* Spring - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/transaction/BeforeTransaction.html[BeforeTransaction]
|
|
* Spring - https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/transaction/AfterTransaction.html[AfterTransaction]
|