Java Reference
In-Depth Information
Because BlueJ allows us to interact directly with individual objects, it offers unique ways to
conduct testing on classes and methods. One of the points we want to stress in this section is
that it is never too early to start testing. There are several benefits in early experimentation and
testing. First, they give us valuable experience with a system; this can make it possible to spot
problems early enough to fix them, and at a much lower cost than if they had not been uncov-
ered until much later in the development. Second, we can start to build up a series of test cases
and results that can be used over and over again as the system grows. Each time we make a
change to the system, these test cases allow us to check that we have not inadvertently intro-
duced errors into the rest of the system as a result of the changes.
In order to illustrate this form of testing within BlueJ, we shall use the online-shop project,
which represents an early stage in the development of software for an online sales shop (such as Our project contains only a very small part of this application, namely the part
that deals with customer comments for sales items.
Open the online-shop project. Currently, it contains only two classes: SalesItem and
Comment . The intended functionality of this part of the application—concentrating solely on
handling customer comments—is as follows:
Sales items can be created with a description and price.
Customer comments can be added to and removed from sales items.
Comments include a comment text, an author's name, and a rating. The rating is in the range
of 1 to 5 (inclusive).
Each person may leave only one comment. Subsequent attempts to leave a comment by the
same author are rejected.
The user interface (not implemented in this project yet) will include a question asking, “Was
this comment helpful to you?” with Yes and No buttons. A user clicking Yes or No is known
as upvoting or downvoting the comment. The balance of up and down votes is kept for com-
ments, so that the most useful comments (the ones with the highest vote balance) can be
displayed at the top.
The Comment class in our project stores information about a single comment. For our testing,
we shall concentrate on the SalesItem class, shown in Code 7.1. Objects of this class repre-
sent a single sales item, including all comments left for this item.
As part of our testing, we should check several parts of the intended functionality, including:
Can comments be added and removed from a sales item?
Does the showInfo method correctly show all the information stored about a sales item?
Are the constraints (ratings must be 1 to 5, only one comment per person) enforced
Can we correctly find the most helpful comment (the one with the most votes)?
We shall find that all of these can be tested conveniently using the object bench within BlueJ. In
addition, we shall see that the interactive nature of BlueJ makes it possible to simplify some of
the testing by making controlled alterations to a class under test.
Search WWH ::

Custom Search