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.

13.4 Installing and Running JUnit297Let’s apply that approach to our previous design discussi<strong>on</strong>. We’ve describedan Account class in our design discussi<strong>on</strong>. It needs a name, an owner,and an amount of m<strong>on</strong>ey when created. It should have a method to createsubaccounts, <strong>on</strong>es that are c<strong>on</strong>nected to this account and get allocated some orall of the main account’s m<strong>on</strong>ey.Example 13.1 is the basic structure of our Account class.That’s enough to begin writing a test. We have described the c<strong>on</strong>structor,with the three parameters that it will need. We’ve also described a method <strong>on</strong>the Account object, <strong>on</strong>e that will create subaccounts. That gives us enoughinformati<strong>on</strong> to write a test that will create an account and then create subaccountsof that account. We can test to see if the accounts are created properly(i.e., are not null) and if the subaccounts use up all the m<strong>on</strong>ey of the parentaccount.When you “test then code,” you begin to use the objects that you havedesigned without getting bogged down in their implementati<strong>on</strong>. You are, ineffect, describing their external interfaces without implementing them. You arealso beginning to use the classes as a user might, though a tester’s use is a bitdifferent than the way an applicati<strong>on</strong> might use them. However, as a user ofthese classes you are beginning to test the design, by testing the results of theuse cases—are these classes really usable?You may discover that you need some additi<strong>on</strong>al functi<strong>on</strong>ality. In our example,we can see from the descripti<strong>on</strong> of our test that we will need a gettermethod <strong>on</strong> the account to return the amount of m<strong>on</strong>ey that remains unallocatedto subaccounts. Then we can test to see if it gets used up properly.There are many more test cases that we could develop for the Accountclass, but let’s use just these for now, so that the size of our test case ismanageable.Our next step is to get JUnit installed before we get too deep intodeveloping our test cases. That will give us something to run these tests.13.4INSTALLING AND RUNNING JUNITIt’s rather simple to install a standal<strong>on</strong>e versi<strong>on</strong> of JUnit. We download a ZIPfile from the JUnit Web site, then unzip it into a directory. Adding the JUnitJAR file to your CLASSPATH is all that’s needed to make JUnit available for youto run it.

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

Saved successfully!

Ooh no, something went wrong!