Java Reference
In-Depth Information
It is also customary (but not required) that the name of the test class ends in Test ,
such as CashRegisterTest . Consider this example:
import junit.framework.TestCase;
public class CashRegisterTest extends TestCase
{
public void testSimpleCase()
{
CashRegister register = new CashRegister();
register.recordPurchase(0.75);
register.recordPurchase(1.50);
register.enterPayment(2, 0, 5, 0, 0);
double expected = 0.25;
assertEquals(expected, register.giveChange(),
EPSILON);
}
public void testZeroBalance()
{
CashRegister register = new CashRegister();
register.recordPurchase(2.25);
register.recordPurchase(19.25);
register.enterPayment(21, 2, 0, 0, 0);
assertEquals(0, register.giveChange(),
EPSILON);
}
// More test cases
. . .
private static final double EPSILON = 1E-12;
}
If all test cases pass, the JUnit tool shows a green bar (see Figure 6 ). If any of the test
cases fail, the JUnit tool shows a red bar and an error message.
Your test class can also have other methods (whose names should not start with test).
These methods typically carry out steps that you want to share among test methods.
JUnit 4 is even simpler. Your test class need not extend any class and you can freely
choose names for your test methods. You use Ȓannotationsȓ to mark the test methods.
An annotation is an advanced Java feature that places a marker into the code that is
interpreted by another tool. In the case of JUnit, the @Test annotation is used to
mark test methods.
Search WWH ::




Custom Search