Over a million developers have joined DZone.
Platinum Partner

Unit Tests - "Written Once and Forgotten Forever"

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

I have come across many unit test cases that are written once and forgotten for ever, with all the dataset/environmental dependencies in it. What is the importance of dataset/environment? Let's take an example, observe the test case below.

public void testEmpFinder () {

//Weird Test case for Fun!!!

String result = "JOHN";

//Passing employee id returns employee object.

//verify the name matches.

Employee emp = EmpFinder.find(1);

assertEquals(emp.getName,result);

}

What's wrong? The developer had made an assumption, that on querying with employeeid='1' will return employee with the name "JOHN". The data could be coming from a database table "EMPLOYEE". But It’s very evident this test case would fail if run on an environment where the employeeid='1' data doesn't exists. This makes the test cases obsolete the moment they are written.

Approach

1. Save Insert/Delete datascript, that could be run on test setup () and delete test data on completion of test.

2. You could use something like DBUnit (http://www.dbunit.org/) which exports and imports the database data into an XML dataset. - Not sure of DBUnit support for modern day ORM's (Hibernate).

Conclusion:

Unit tests should match & sustain life time of source, after all its a guarantee card to your source code. We just looked at database dependency for example, how about dependencies such as JMS, Content Repository, LDAP etc. Writing a test case with all its data & environmental dependencies Mocked & externalized is an Art.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}