Monday, September 10, 2007

Taking Exception with Exceptions

Exceptions in Java are thrown when an abnormal condition arises. Exceptions not handled by a method are passed (via the throws keyword) to its parent. More often that not, test suites focus on the normal behavior of a method, and skip over the cases where the unhandled exceptions are thrown. A comprehensive unit testing effort should test each error condition separately. Thus, fewer thrown exceptions means fewer individual tests will be needed.

Consider a method with this signature:

public void foo() throws Exception1, Exception2, Exception3

What if there's a bug in this method that causes Exception1 to be thrown instead of Exception2? In order to fully test this method, our test suite needs to include at least three JUnit tests to confirm that the proper exception is thrown when the corresponding abnormal condition arises.

Even if the correct type of exception is thrown, it may be the case that all three exception types are handled the same way, or it may be that some of the exceptions can be handled by the method itself. If we can remove even one of the thrown exception types, we'll reduce our testing effort by one-third. There is no shortcut for reducing the number of thrown exceptions, of course -- indeed, each distinct exception may be necessary -- but by keeping in mind the number of thrown exceptions as you code, you may be able to streamline your testing effort down the road.

Labels: , , ,

Sunday, September 09, 2007

Lose Those Extra Parameters

Most unit testing strategies focus on a method's parameters. By definition, they control the behavior of the method under test, so varying the input values of the method, one can build a strong test suite. But the more parameters that your method under test has, the more tests that will be required to do complete black-box testing. Obviously, most methods take one or more parameters, but if there are clear ways to limit the number of parameters, it will be a clear win for black-box testing scenarios.

Consider this method signature:

public int addOrSubtract(int x, int y, Operator operator)

Each additional parameter increases the number of possible combinations that you'd likely want to test. If you try three possible values for each parameter, that's 27 tests for this method. If there are only two possible operators (addition and subtraction), we could refactor this method into two simpler ones:

public int add(int x, int y)
public int subtract(int x, int y)


Now, even if we try three possible values for each parameter, we still only wind up with nine combinations for each method, or 18 total -- we've reduced our testing effort by one-third. Obviously, there is no cut-and-dried method for reducing the number of parameters, and there may indeed be cases where your method under test does require many parameters, but by keeping the method's signature to as few parameters as possible, you'll save yourself testing effort down the road.

Labels: , ,

Monday, July 23, 2007

Seven Metrics to Improve Your Unit Testing

Unit testing is tough, regardless of manual or automated, test-first or test-after. With any of the approaches, there are certain metrics to pay attention to while you code. Staying within acceptable levels means your code will be much more testable, whether you unit test or not!

  • Parameters - Each parameter added to a method's signature increases the number of possible ways that method can be invoked, eventually creating an unmanageable combinatorial problem. By limiting the number of parameters to three or fewer, you can easily increase the testability of your code.
  • Exceptions - Each thrown exception represents a distinct error condition that must be tested, and error conditions are very hard to test. Keeping the number of thrown exceptions to three or less greatly simplifies your testing effort.
  • Cyclomatic Complexity - Cyclomatic Complexity (CC) is a measure of the decision logic in a method. The more decisions your method has, the more ways it will behave. Keeping the number of CC paths to 10 or less greatly reduces the risk of defects as well as increases the overall testability of your code.
  • Lines of Code - One of the oldest and simplest metrics used to measure a method's testability. A shorter method is easier to understand and test.
  • Paths - This metric looks at distinct data paths. Data paths are paths that link a data element's definition to its use. In other words, if you define a data element, you should use it, and if you use it, you should test it. Data paths often overlap with Cyclomatic Complexity paths.
  • Static Invocations - Methods under test invoke methods from external classes, and these external classes can usually be mocked or dummied up to behave exactly as required for an individual unit test. Static invocations of external methods often have an unchangeable behavior that thwarts even the best testing effort. Avoid this unnecessary coupling to external classes, and keep your external static invocations to a minimum.
  • Anonymous Classes - Anonymous classes make unit testing difficult because they exist only within the method under test, and cannot themselves be unit tested. Although anonymous classes are convenient (especially for GUI programming), their lack of testability and mockability can hinder your testing effort.

Labels: , ,