Sunday, June 10, 2012

Testing @Transactional

Do you use Spring? Then chances are that you use @Transactional in your code.

@Transactional is very clever, but would it not be smart if some unit test failed if someone accidentally removed the @Transactional annotation?

In the code for the seminar Spring Dependency Injection Styles the author Chris Beams has this unit test:

As you can see this method will test whether the transferService implementation (being injected further up the test class) is transactional by checking whether it is being advised by the TransactionInterceptor (consult the Spring documentation on transactions if you are unsure about why this makes sense).