Download presentation
Presentation is loading. Please wait.
Published byKellie Simon Modified over 9 years ago
1
Chapter 2 Additional Features of Java Programming
6
METHOD TESTING WITH JUnit
7
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.
8
JUnit is a free software package for testing a class’s methods.
9
For example, here is an indirect test of the 3-parameter constructor in the HourlyEmployee class:
10
@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
11
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
12
@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 test1 @Test 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
13
@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 @Test 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
14
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.
15
Exception Handling
16
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
63
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 $700.00 in both cases.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.