Java Reference
In-Depth Information
Let's apply that approach to our previous design discussion. We've de-
scribed an Account class in our design discussion. It needs a name, an owner,
and an amount of money when created. It should have a method to create
subaccounts, ones that are connected to this account and get allocated some or
all of the main account's money.
Example 13.1 is the basic structure of our Account class.
That's enough to begin writing a test. We have described the constructor,
with the three parameters that it will need. We've also described a method on
the Account object, one that will create subaccounts. That gives us enough
information to write a test that will create an account and then create subac-
counts of 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 money of the parent
account.
When you “test then code,” you begin to use the objects that you have
designed without getting bogged down in their implementation. You are, in
effect, describing their external interfaces without implementing them. You are
also beginning to use the classes as a user might, though a tester's use is a bit
different than the way an application might use them. However, as a user of
these classes you are beginning to test the design, by testing the results of the
use cases—are these classes really usable?
You may discover that you need some additional functionality. In our ex-
ample, we can see from the description of our test that we will need a getter
method on the account to return the amount of money that remains unallocat-
ed to 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 Account
class, but let's use just these for now, so that the size of our test case is
manageable.
Our next step is to get JUnit installed before we get too deep into
developing our test cases. That will give us something to run these tests.
13.4
I NSTALLING AND R UNNING JU NIT
It's rather simple to install a standalone version of JUnit. We download a ZIP
file from the JUnit Web site, then unzip it into a directory. Adding the JUnit
JAR file to your CLASSPATH is all that's needed to make JUnit available for you
to run it.
Search WWH ::




Custom Search