Tuesday, March 9, 2010

Using JTestcase for unit testing - Part I

Test Driven Development (TDD) has become a very critical element in today's development with systems being more complex and time-lines and budget as tight as if not more tighter than ever. This has caused more and more unit testing frameworks and support libraries to be released. JTestcase is one such library that helps in separating test data from test cases.

While almost everyone appreciates the benefit of unit testing, when it comes to actually developing the test cases, there are various deterring factors that quickly make us lose interest in writing unit tests. One those factors is the test data. While test data for simple computations may be very easy to build, things become tough when we are dealing with domain objects that are huge and consist of complex fields within them. The mere size of them and the code that gets interspersed into the unit test code to create them adds a lot of clutter to the unit test code, making the code tedious to follow and maintain.

JTestcase helps us to separate the data from the unit test code into XML. It helps create simple domain objects easily and uses a part of JICE framework for building complex object trees. JTestcase has facilities to represent assertions in addition to test input and output data. JTestcase XML structure is as follows:

Class to be tested 1
......Method to be tested 1
...........Test case 1:
......................Param 1
......................Param 2
......................Assertion 1
......................Assertion 2
............Test Case 2
.
.
......Method to be tested 2
.
.
.

While JTestcase has support for this structure, I have found that another way of organizing test data using JTestcase seemed to work well and seemed to be in line with the expectations of various teams that I have worked with. In the subsequent parts I intend to blog the conventions that we followed when depicting data using JTestcase (along with examples),which worked very well for us.

In the meanwhile, following are the links to this framework and JICE:

JTestCase
JICE Engine