Java Reference
In-Depth Information
APPENDIX
G
Unit unit-testing tools
In this appendix, we give a brief outline of the main features of BlueJ's support for JUnit-style
unit testing. More details can be found in the testing tutorial that is available from the topic's
CD and the BlueJ web site.
G.1
Enabling unit-testing functionality
In order to enable the unit-testing functionality of BlueJ, it is necessary to ensure that the
Show
unit testing tools
box is ticked under the
Tools-Preferences-Miscellaneous
menu. The main
BlueJ window will then contain a number of extra buttons that are active when a project is open.
G.2
Creating a test class
A test class is created by right-clicking a class in the class diagram and choosing
Create Test
Class
. The name of the test class is determined automatically by adding
Test
as a suffix to the
name of the associated class. Alternatively, a test class may be created by selecting the
New
Class …
button and choosing
Unit Test
for the class type. In this case, you have a free choice
over its name.
Test classes are annotated with
<<unit test>>
in the class diagram, and they have a color
distinct from ordinary classes.
G.3
Creating a test method
Test methods can be created interactively. A sequence of user interactions with the class dia-
gram and object bench are recorded and then captured as a sequence of Java statements and
declarations in a method of the test class. Start recording by selecting
Create Test Method
from
the pop-up menu associated with a test class. You will be prompted for the name of the new
method. Earlier versions of JUnit, up to version 3, required the method names to start with the
prefix “test”. This is not a requirement in current versions. The
recording
symbol to the left of
the class diagram will then be colored red, and the
End
and
Cancel
buttons become available.
Once recording has started, any object creations or method calls will form part of the code of
the method being created. Select
End
to complete the recording and capture the test, or select
Cancel
to discard the recording, leaving the test class unchanged.
Test methods have the annotation
@Test
in the source of the test class.
Search WWH ::
Custom Search