Skip to main content


Showing posts from November, 2011

Spring pitfalls: transactional tests considered harmful

One of the Spring killer-features is an in-container integration testing . While EJB lacked this functionality for many years (Java EE 6 finally addresses this, however I haven't, ekhem , tested it), Spring from the very beginning allowed you to test the full stack, starting from web tier, through services all the way down to the database. Database is the problematic part. First you need to use in-memory self-contained database like H2 to decouple your tests from an external database. Spring helps with this to a great degree, especially now with profiles and embedded database support . The second problem is more subtle. While typical Spring application is almost completely stateless (for better or worse), database is inherently stateful. This complicates integration testing since the very first principle of writing tests is that they should be independent on each other and repeatable. If one test writes something to the database, another test may fail; also the same test may