Home Segments Top Top Previous Next

751: Mainline

Of course, testing code may become complex. To avoid cluttering your class definitions with complex testing code, and to save space in your compiled code, you should define classes specifically for testing. The following tests the basic Meter class shown in Segment 748, without defining a cluttering main method inside the Meter definition:

In general, you should define your testing class to be a subclass of the tested class, so that you may, if you wish, define shadowing methods for testing purposes.

import javax.swing.*;                            
public class MeterTestor extends Meter { 
 public static void main (String argv []) { 
  JFrame frame = new JFrame("Meter Test"); 
  MeterTestor meter = new MeterTestor(0, 100); 
  frame.getContentPane().add("Center", meter); 
  frame.setSize(350, 150); 
  frame.addWindowListener(new ApplicationClosingWindowListener()); 
  frame.show(); 
  meter.setValue(25); 
  meter.setTitle("Meter Test"); 
 } 
 public MeterTestor (int x, int y) { 
  super(x, y); 
 } 
}