I have an api class annotated with @Transactional
as shown. Some subset of the methods are further annotated with readOnly=true
. When writing tests is there any way to verify the number of transactions that actually take place? Consider if the delete
method implementation called the exist
method - then in a test calling delete
only one transaction should occur. However, if the test itself calls the exist
method prior to calling delete
then 2 transactions should take place.
@Transactional
public class AnApi {
public AnEntity create(EntityData data) {...}
public void delete(Long id) {...}
@Transactional(readOnly = true)
public boolean exists(Long id) {...}
}
While I know the framework functions this way, it is preferable to put in tests to detect any future changes and avoid potential surprises.
That shouldn't be too difficult.
Spring uses a PlatfromTransactionManager
for managing transactions.
If you register a wrapped transaction manager that implements this interface by delegating to the actual transaction manager, you should be easily able to count the invocations/number of transactions returned.