JUnit Testing with BlueJ
CPE 102 Lab Activity
Goal: Learn to create and run JUnit tests using the BlueJ IDE.
- If you haven't setup your account to run BlueJ, then make
a launcher for BlueJ.
- Launch the BlueJ IDE.
(If the display is disturbed by horizontal streaking, choose
"System -> Preferences -> Desktop Effects" and click
"Enable desktop effects.")
- From the "Tools" menu, select Preferences. Click the
"Miscellaneous" tab. Check "Show unit testing tools." Click
"OK". Now you will see a new button in the IDE labeled "Run
Tests".
- Download
people.zip and unzip it on your hard drive. It
creates a BlueJ project folder named "people".
- From the "Project" menu, select "Open Project" and open the
people project.
- Open this Unit
Testing Tutorial (pdf). Read sections 1 and 2, then
complete the activities in sections 3 - 6. (Estimated
time: 15 minutes) There is nothing to submit for this
section.
- From the "Project" menu select "New Project". (You may name it
"JUnitIntro".)
- Click the "New Class" button and create a new Java class named
Rope.
- Right-click on the new created Rope class
in the
diagram and select "Open Editor".
- Copy and paste the source code from this skeleton file
into the editor window.
- Click the "Compile" button. Notice the error message
that
appears because the class is incomplete.
- Enhance the source code by providing the missing method bodies
where indicated.
- Compile your completed class and correct any errors.
- Provide a separate
@author
tag for each
student.
- Close the editor window.
- Right-click on the Rope class
in the diagram and
select "Create Test Class". Note that a new class is
created
named RopeTest.
- Right-click on the new created RopeTestclass
in
the diagram and select "Open Editor". Study the skeleton
that was
automatically created for you.
- For today's activity the instructor has provided some example
tests. Study the example tests.
- Replace all the auto-generated code in RopeTest by pasting the source code
from the example
tests into the editor window.
- Click the "Compile" button. The tests should compile
without error.
- In the project window, click the "Run Tests" button.
Observe the results of running the tests. One of the tests
fails
because the test is formulated incorrectly. Change
"Varoom" to
"Zoom" and the test should pass.
- Create a zip file containing both the source code and the
JUnit
tests. You can do this in the file explorer by selecting the two
files
and then right-clicking and selecting "Create Archive." Be sure
the
JUnit class name ends in "Test" or Web-CAT won't realize it's a
JUnit class.
Be sure "Zip" is selected in the file type selection box.
Prerequisite: The instructor must have created an
account for you on
Web-Cat.
- Login to Web-CAT,
being sure to select "Cal Poly" as the institution. Submit the
zip file to Web-CAT for assignment "JUnit Intro".
- Study the Web-CAT results online.
-
"Results from Running Your Tests" should be 100% because you
got them
running in BlueJ (above).
-
"Code Coverage from Your Tests" shows the effectiveness of
the unit tests
that you submitted.
-
"Estimate of Problem Coverage" shows how many of the
instructor-provided
tests you passed.
- You will probably find that the instructor-provided tests on
Web-Cat are more thorough than the example tests provided.
You'll likely find one or more failed tests. In each case, look for the hint about which tests case failed.
Make corrections in the Rope class until "Problem Coverage" gives 100%.
- In addition,
you'll need to improve the tests you were given.
You'll need to add more test cases to thoroughly exercise your program code.
- Correct any errors and resubmit your work until it the grader
gives it a perfect score. Your code must run correctly and your
tests must demonstrate
complete statement coverage. Your code does not have to
conform to the class coding standard.
Tip: You'll need these JUnit methods:
assertEquals,
assertTrue,
assertFalse
- If you are new to the BlueJ IDE, do a fast read of
The BlueJ Tutorial(pdf).
References:
Unit
Testing
Tutorial
Junit
and BlueJ (Video)