Incremental Java
Testing a Class

How to Test a Class

We're in the process of writing a class called Rectangle. However, suppose someone had already written it, and you wanted to use it. How could you do this?

One way to do this is to write a test class. As far as the compiler is concerned, a test class looks like any other class. It has no idea what a test class.

For us, the goal is to create Rectangle objects, and call some methods to see how well it works.

To do this, we create a class called RectangleTest. We have no intentions of making any objects of this type. Instead, we intend to have a main() and call methods in main().

Let's look at an example.

Writing RectangleTest

We see public static void main() once again!
public class RectangleTest
{
   public static void main( String [] args )
   {
       Rectangle rect = new Rectangle() ;
       System.out.println( "rect has height " + rect.getHeight() ) ;
       System.out.println( "rect has width " + rect.getWidth() ) ;
       System.out.println( "rect has area " + rect.getArea() ) ;
       System.out.println( "rect has perimeter " + rect.getPerimeter() ) ;
       System.out.println( "rect is square? " + rect.isSquare() ) ;

       rect.setHeight( 3 ) ;
       System.out.println( "rect has height " + rect.getHeight() ) ;
       System.out.println( "rect has width " + rect.getWidth() ) ;
       System.out.println( "rect has area " + rect.getArea() ) ;
       System.out.println( "rect has perimeter " + rect.getPerimeter() ) ;
       System.out.println( "rect is square? " + rect.isSquare() ) ;

       rect.setWidth( 10 ) ;
       System.out.println( "rect has height " + rect.getHeight() ) ;
       System.out.println( "rect has width " + rect.getWidth() ) ;
       System.out.println( "rect has area " + rect.getArea() ) ;
       System.out.println( "rect has perimeter " + rect.getPerimeter() ) ;
       System.out.println( "rect is square? " + rect.isSquare() ) ;

       rect.setWidth( 3 ) ;
       System.out.println( "rect has height " + rect.getHeight() ) ;
       System.out.println( "rect has width " + rect.getWidth() ) ;
       System.out.println( "rect has area " + rect.getArea() ) ;
       System.out.println( "rect has perimeter " + rect.getPerimeter() ) ;
       System.out.println( "rect is square? " + rect.isSquare() ) ;
    }
}
You should be able to guess what this prints.

The idea of a test class is to try out all the methods, and for now, we print out the results. Later on, we'll have better ways to test.

When we're in main() in this class, we are an object user. We make objects, and we call methods on the object. We don't care how the object does what it does, as long as it does it correctly.

We have fewer privileges than the class designer, who gets to control the insides of the object, but that's fine with us. We want

Test Driven Development

In recent years, programming evangelists (people who have ideas about programming and want to tell others about it) have suggested that testing should come before coding.

If you've done any programming before, you'd think maybe that's crazy. However, there is some sense in writing a test class even before the class you're testing is finished being written.

One reason is to think about the class as the user thinks about it. You write out real code, and see how it behaves. Of course, since the class hasn't been written, you won't be able to test it until it's done.

Later on, once you've learned how to write a complete class definition, we'll talk about creating stubs, which is a quick and dirty way to write a class (that doesn't work). Amazingly, this can be useful.

Stubs

One way to write a class that doesn't work very quickly is to write stub functions. Basically, you return the simplest value for return type. For example, 0 for int, 0.0 for double, and true for boolean. Maybe "" for String.

Here's how to write