Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Test for fun and profit, Part 2: Unit testing

Learn the ins and outs of testing in a Java environment

  • Print
  • Feedback
Can a programmer function as an effective tester? I believe so -- but it requires us to don a cap of a different color and think about interacting with our application in a very different way. In October's How-To Java I outlined the principles of software testing and tried to convey its importance in the development of high-quality software. This month I assume a slightly more aggressive stance to decimate any notions you may have that testing is best done in an informal, ad hoc fashion. Unpleasant as testing may be, it must be done.

Let's face it: we're developers. We want to create, not test. Any testing we do typically consists of taking our creation for a spin around the track and fixing the resulting flat tires. Once we turn the car over to someone else ... well, as they say, Out of sight, out of mind.



Unfortunately, what I've just described is not testing -- it's not even a close cousin. Testing as an endeavor requires intellectual effort and methodical preparation. Remember, successful tests uncover defects. I hope, when you've finished reading, you understand the challenge behind devising successful tests, because you will then understand why testing tools must be both complete (so you can build thorough tests) and as easy to use and unobtrusive as possible (so they don't themselves complicate the process). Next month we'll begin to build a test harness of our own.

Unit testing

Last month you learned a little bit about many different types of tests and testing strategies: unit and component tests, integration tests, system tests, and so on. This month, we'll focus our attention on the first of these: unit (and component) tests.

I think the best way to get a handle on the notion of unit testing is to consider the simplest programmatic unit in a Java program -- a single class.

public
class Foo {
  private
  HashMap _hashmap = new HashMap();
  private
  Service _service = null;
  public
  Foo(Service service) {
    _service = service;
  }
  public
  Handle
  add(Node node) {
    Handle handle = _service.register(node);
    _hashmap.put(handle, node);
    return handle;
  }
  public
  void
  nestedToString(String stringPrefix, StringBuffer stringbuffer)
  throws EvaluationException {
    stringbuffer.append(stringPrefix);
    stringbuffer.append(evalutate());
    stringbuffer.append('\n');
      .
      .
      .
  }
  public
  abstract
  Object
  evaluate()
  throws EvaluationException {
    .
    .
    .
  }
}


Keep that class in mind as we continue our discussion.

Philosophically, unit testing looks at a programmatic unit, such as a class, as a box with a set of inputs and a set of outputs through which all communication between the box and its environment must flow.

Unit testing: The concept



The figure above depicts a class loosely modeled as one such idealized box. Theoretically, we should be able to apply a set of one or more inputs to a unit, observe the outputs in each case, and thereby determine whether the unit is functioning correctly. The unit-test approach thus presents a nice, simple, logically sound model.

  • Print
  • Feedback

Resources
  • "Runnability testing of Java Programs," Roger Hayes and Manoochehr Ghiassi (JavaWorld, August 1998), explains how to apply and adapt standard quality-assurance techniques to efficiently assure the runnability of your programs
    http://www.javaworld.com/javaworld/jw-08-1998/jw-08-runtest.html
  • Reliable Software Technologies hosts the comp.software.testing FAQ, a good source for testing-related information
    http://www.rstcorp.com/c.s.t.faq.html
  • The comp.software.testing newsgroup features discussions on testing-related topics and provides an excellent source of answers and ideasnews://comp.software.testing/