Steve Chambers Steve Chambers - 1 month ago 4
Java Question

How to remove non-transactional database logging after running integration tests?

I'm working on a Java/Spring app that requires audit logs to be written to a database. All services are currently marked as

@Transactional
- so if there is a failure, the changes are rolled back.

But audit logging is the exception to this - it should always succeed - so have been considering marking the AuditLogService as either
Propagation.NOT_SUPPORTED
or
Propagation.REQUIRES_NEW
.

The question is how to craft the integration tests. Ideally these should not leave log entries in the database. Would prefer not to have to manually delete this at the end of the test. Is there perhaps a way of marking a test as transactional that would include all transactions including ones that have started via
Propagation.REQUIRES_NEW
?

Answer

I ended up doing exactly what I said I didn't want to do and deleting all the operational data at the end of each test. (This actually worked better as the tests were no longer run in an overarching transaction, which masked some bugs, e.g. relating to Hibernate lazy loading.)

Comments