Chapter 2 Additional Features of Java Programming
METHOD TESTING WITH JUnit
A method is correct if it satisfies its specification. Your confidence in the correctness of your methods is increased by testing, which can reveal the presence but not absence of errors.
JUnit is a free software package for testing a class’s methods.
For example, here is an indirect test of the 3-parameter constructor in the HourlyEmployee class:
@Test public void test3() { hourly = new HourlyEmployee ("terry", 41, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (30.00, hourly.getOvertimePay()); assertEquals (830.00, hourly.getGrossPay()); } // method test3
import org.junit.*; import static org.junit.Assert.*; import org.junit.runner.Result; import static org.junit.runner.JUnitCore.runClasses; import java.util.*; public class HourlyEmployeeTest { public static void main(String[ ] args) { Result result = runClasses (HourlyEmployeeTest.class); System.out.println ("Tests run = " + result.getRunCount() + "\nTests failed = " + result.getFailures()); } // method main
@Test public void test1() { hourly = new HourlyEmployee ("andrew", 39, 10.00); assertEquals (390.00, hourly.getRegularPay()); assertEquals (0.00, hourly.getOvertimePay()); assertEquals (390.00, hourly.getGrossPay()); } // method public void test2() { hourly = new HourlyEmployee ("beth", 40, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (0.00, hourly.getOvertimePay()); assertEquals (800.00, hourly.getGrossPay()); } // method test2
@Test public void test3() { hourly = new HourlyEmployee ("terry", 41, 20.00); assertEquals (800.00, hourly.getRegularPay()); assertEquals (30.00, hourly.getOvertimePay()); assertEquals (830.00, hourly.getGrossPay()); } // method public void test4() { hourly = new HourlyEmployee ("karen", 50, 10); assertEquals (400.00, hourly.getRegularPay()); assertEquals (150.00, hourly.getOvertimePay()); assertEquals (550.00, hourly.getGrossPay()); } // method test4 } // class HourlyEmployeeTest
TESTING PRINCIPLES Testing can reveal the presence of errors but not the absence of errors. A method’s tests should be developed before the method is defined. In general, methods should be designed to facilitate testing. Make sure that any boundary conditions are thoroughly tested. Good testing requires great skepticism.
Exception Handling
A robust program is one that does not terminate abnormally from invalid user input. An exception is an object created by an unusual condition, such as an attempt at invalid processing
Exercise: Define an equals method in the HourlyEmployee class to override the equals method in the Object class. Two HourlyEmployee objects are equal if they have the same name, hours worked and pay rate (but pay rates should not be compared for equality). Hint: If there is an HourlyEmployee object whose name is “Smith”, with 60 hours worked and a pay rate of $10.00 per hour, that employee should not be equal to another HourlyEmployee named “Smith” with 35 hours worked and a pay rate of $20.00 per hour. The gross pay is $ in both cases.