Java Reference
In-Depth Information
attempting to add a comment with an author who has already submitted a comment
attempting to add a comment with an invalid rating (negative)
The first of these already exists in the SalesItemTest class. We will now describe how to cre-
ate the next one using the online-shop-junit project.
A test is recorded by telling BlueJ to start recording, performing the test manually, and then
signaling the end of the test. The first step is done via the menu attached to a test class. This tells
BlueJ which class you wish the new test to be stored in. Select Create Test Method …from the
SalesItemTest class's pop-up menu. You will be prompted for a name for the test method.
By convention, we start the name with the prefix “test.” For example, to create a method that
tests adding two comments, we might call that method testTwoComments . 1
Once you have entered a name and clicked OK, a red recording indicator appears to the left of
the class diagram, and the End and Cancel buttons become available. End is used to indicate the
end of the test-creation process and Cancel to abandon it.
Once recording is started, we just carry out the actions that we would with a normal manual
Create a SalesItem object.
Add a comment to the sales item.
Once addComment has been called, a new dialog window will appear (Figure 7.5). This is an
extended version of the normal method result window, and it is a crucial part of the automated
testing process. Its purpose is to allow you to specify what the result of the method call should
be. This is called an assertion .
Figure 7.5
The Method Result
dialog with assertion
1 Earlier versions of JUnit, up to version 3, required the method names to start with the prefix “test.” This
is not a requirement anymore in current versions.
Search WWH ::

Custom Search