Posts

Showing posts from August, 2012

Grails: testing rollback of transactional service methods

Grails is so easy to dive into, but one quickly starts to encounter apparently nebulous corner cases. One such case is the testing of transactional rollback or a service method. Take the following example: class SimpleService { saveMyDog(Dog dog){ dog.save() throw new RuntimeException("don't save my doggie") } } Service methods are bound to a transaction by default, so because of the RuntimeException being thrown, in the above method the dog should not be saved/persisted.Not transactional behaviour is database dependent. Be sure you're using InnoDB!Now lets test the rollback behaviour we expect from our method class SimpleServiceTests extends GroovyTestCase { def simpleService void testSaveMyDog(){ def existingDogCount = Dog.count() simpleService.saveMyDog(new Dog()) def dogCountStayedTheSame = existingDogCount == Dog.count() ? true : false assert dogCountStayedTheSame } } The above test fails. What ?? Our rollback doesn't work…