Java Reference
In-Depth Information
// run before each test case:
protected void
setUp()
{
base = new Account("Base", new User("testuser"), "150");
}
// our one test case
public void
testCreateSub()
{
// Create a subaccount, assigning $50 of our pool of $150.
Account sub1 = base.createSub("sub1", "50");
// Make sure that it created something.
assertNotNull("Couldn't create sub1", sub1);
// Now a 2nd subaccount.
Account sub2 = base.createSub("sub2", "75");
assertNotNull("Couldn't create sub2", sub2);
// Now a 3rd subaccount, to use up all the $.
Account sub3 = base.createSub("sub3", "25");
assertNotNull("Couldn't create sub3", sub3);
// We should have the same total that we started with.
assertEquals(150, base.getTotal().getDollars());
// We should have used up all our $.
assertEquals(0, base.getBalance().getDollars());
// Be sure the (sub)account lookup works:
Account ex2 = base.getSub("sub2");
assertNotNull("Couldn't find sub2", ex2);
assertSame(sub2, ex2);
} // testCreateSub
} // class AccountTest
Notice how we've named our test case class. We take the name of the class
and append Test to the end. This is convenient for us—we can easily see which
classes have test cases; but more importantly, JUnit can use this and other
naming conventions to derive the test case names (more on that later). Notice
also that the method in the Account class that we want to test, called
createSub() , gets exercised by a method named testCreateSub() —we
Search WWH ::




Custom Search