Thursday, August 24, 2006

Unit Testing Problem

I previously wrote about keeping Unit tests as stand-alone tests. This is the correct practice, However, doing so can sometimes cause a problem.

Problems occur when trying to test a boundary object, especially if it has complicated rules about how it works. The best thing to do in this instance is to replace the boundary object with a Mock Object that implements the expected results. If you don't plan for it, this can cause a shift in your design. For example:
puclib class ClassToTest
private boundaryClass myBoundary;

This is not good anymore. This code specifically ties ClassToTest to the boundary class. However, the boundary class could have the problems. Instead ClassToTest should have the boundary object passed to it, and code to an interface.

public class ClassToTest
private IboundaryClass myBoundary;
public ClassToTest(IboundaryClass boundaryClassItem)
myBoundary = boundaryClassItem;

This is a good chunk of code now, because boundaryClassItem can be either the actual boundary item (as it will in the deployed program), or it can be a mock object (which it will be in Unit Testing).

In my particular case, the login function being called by each test is a problem. However, for the purpose of testing each as a stand-alone item everything works fine.

The problem is that Login can only be called once nicely in on the boundary class and have the process work correctly. So NUnit itself is actually breaking my boundary class. However, a Mock class that allows that will allow NUNit to run successfully. I will at least have the confidence that everything except my boundary class works correctly, and then just try to fix bugs if they occur.

No comments: