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

@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

@Transactional
public class TransactionalTest {

    @BeforeTransaction
    public void setupTransaction() {
        // Setup logic
    }

    @AfterTransaction
    public void cleanupTransaction() {
        // Cleanup logic
    }
}

Resources

Documentation