jump to navigation

Feeling like an idiot September 3, 2008

Posted by Phill in Spring, Testing.
Tags: , , ,

Do you ever spend a whole morning tracking down a problem, only to find out that it was your own stupid fault in the first place? I do. I did this morning.

I wrote a unit test a while back which tries to insert some data into a database (in a transaction), and then the transaction should be rolled back. (The transactions are handled automagically by AbstractTransactionalSpringContextTests). It all seemed to be working fine – the data was being inserted OK and then rolled back.

However, this morning I noticed that the data was not being rolled back. “That’s strange”, I thought, “I’m sure it was working a few weeks ago…”. I tried turning on debug logging. Spring made absolutely no mention of transactions for those particular tests.

I tried a number of things, including messing around with my Spring configuration – nothing!

Long story short, it turns out that the problem was incredibly simple:

In my unit test class, I’d overridden onSetUp, but hadn’t called super.onSetUp(). The AbstractTransactionalSpringContextTests class makes use on onSetUp to start the transaction. Doh! So, a quick call to super.onSetUp() at the start of my onSetUp method and everything seems to be working again…

This just demonstrates, though – always be sure what you’re overriding before you override it!

%d bloggers like this: