13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

304Chapter 13JUnit: Automating Unit Testing// run before each test case:protected voidsetUp(){base = new Account("Base", new User("testuser"), "150");}// our <strong>on</strong>e test casepublic voidtestCreateSub(){// 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 AccountTestNotice how we’ve named our test case class. We take the name of the classand append Test to the end. This is c<strong>on</strong>venient for us—we can easily see whichclasses have test cases; but more importantly, JUnit can use this and othernaming c<strong>on</strong>venti<strong>on</strong>s to derive the test case names (more <strong>on</strong> that later). Noticealso that the method in the Account class that we want to test, calledcreateSub(), gets exercised by a method named testCreateSub()—we

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!