jump to navigation

JUnit, Spring, and database transactions March 9, 2009

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

Quick post: I have a suite of tests running against the database. All of these tests need to run in a transaction – which is then rolled back at the end.

However, due to unusual circumstances I needed one specific test to run without a transaction. It turned out this is fairly easy to do in Spring, using AbstractTransactionalDataSourceSpringContextTests (a class Spring provides for running tests in transactions). You just need to override the runBare method:

public void runBare() throws Throwable
{
  if (getName().equals( "methodName" ))
  {
     preventTransaction();
  }

  super.runBare();
}

Easy when you know how! There was nothing in the docs detailing this code exactly, but I managed to figure it out with a combination of javadocs and reading the source code (probably unnecessary, but still).

Advertisements
%d bloggers like this: