Functional Junit Test - Part 1 - Database Rollback

Unit tests should be isolated and repeatable. Many are using mock frameworks like JMock, EasyMock etc. And those test cases should not interact with other layers (e.g. network services or database, etc).

This post is not going to talk about those mocking frameworks. Instead, I'll share with you some of my experience working on integration test: functional Junit test cases that interact with database server.

This is not a place to debate whether dependencies should be mocked out or not.

Our development settings are Eclipse + TopLink + Oracle and test cases run in a local environment (not JTA environment). I'll publish a series of posts in my blog about functional Junit test.

Database Rollback

This 1st post is about database rollback, which makes Junit test cases repeatable. Implementation is very simple. In the tearDown(), you just need to call
  1. oracle.toplink.sessions.UnitOfWork.release().
  2. oracle.toplink.sessions.Session.release().
with the assumption that the TopLink session and unit of work is being created somewhere in your application code, e.g. when registering a new object into TopLink or when retrieving data from the database. By calling the above two methods, you are basically rolling back all the changes made by your test cases. In my projects, we used a base test case to host the rollback logic.

No comments: