Java Reference
In-Depth Information
import org.junit.Test
import org.junit.Assert;
public class CashRegisterTest
{
@Test public void simpleCase()
{
register.recordPurchase(0.75);
register.recordPurchase(1.50);
register.enterPayment(2, 0, 5, 0, 0);
double expected = 0.25;
Assert.assert Equals (expected,
register.giveChange(), EPSILON);
}
// More test cases
. . .
}
373
374
The JUnit philosophy is simple. Whenever you implement a class, also make a
companion test class. You design the tests as you design the program, one test method
at a time. The test cases just keep accumulating in the test class. Whenever you have
detected an actual failure, add a test case that flushes it out, so that you can be sure
that you won't introduce that particular bug again. Whenever you modify your class,
simply run the tests again.
The JUnit philosophy is to run all tests whenever you change your code.
If all tests pass, the user interface shows a green bar and you can relax. Otherwise,
there is a red bar, but that's also good. It is much easier to fix a bug in isolation than
inside a complex program.
S ELF C HECK
21. Provide a JUnit test class with one test case for the Earthquake class
in Chapter 5 .
22. What is the significance of the EPSILON parameter in the
assertEquals method?
Search WWH ::




Custom Search