Simple transactional unit testing database
Last week I wrote a post about using Google Guice to perform transaction handling using interceptors . When it came time to write some database level unit tests, I wanted to have the same kind of simple transaction handling so that every unit test is rolled back on completion. Rolling back the changes each test makes is necessary for the tests to be repeatable and consistent . TestNG provides convenient way to do processing before and after each method is called using @BeforeMethod and @AfterMethod . All you need to do is annotate methods that you want automatically rolled back with @Transactional then check for it and act accordingly. The same could be done with JUnit using a RunListener . Below is my implementation. I use just one connection per unit test, it can easily be adapted across multiple connections. public abstract class BaseUtilTest { private static final Logger log = Logger.getLogger(BaseUtilTest.class); protected Connection con; @BeforeClass public void init()