JUnit Testing with BlueJ

CPE 102 Lab Activity


Goal: Learn to create and run JUnit tests using the BlueJ IDE.
  1. If you haven't setup your account to run BlueJ, then make a launcher for BlueJ.

  2. Launch the BlueJ IDE.
    (If the display is disturbed by horizontal streaking, choose "System -> Preferences -> Desktop Effects" and click "Enable desktop effects.")

  3. 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".

  4. Download people.zip and unzip it on your hard drive.  It creates a BlueJ project folder named "people".

  5. From the "Project" menu, select "Open Project" and open the people project.

  6. 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.



  7. From the "Project" menu select "New Project". (You may name it "JUnitIntro".)

  8. Click the "New Class" button and create a new Java class named Rope.

  9. Right-click on the new created Rope class in the diagram and select "Open Editor".

  10. Copy and paste the source code from this skeleton file into the editor window.

  11. Click the "Compile" button.  Notice the error message that appears because the class is incomplete.

  12. Enhance the source code by providing the missing method bodies where indicated.

  13. Compile your completed class and correct any errors.

  14. Provide a separate  @author tag for each student.

  15. Close the editor window.

  16. Right-click on the Rope class in the diagram and select "Create Test Class".  Note that a new class is created named RopeTest.

  17. Right-click on the new created RopeTestclass in the diagram and select "Open Editor".  Study the skeleton that was automatically created for you.

  18. For today's activity the instructor has provided some example tests.  Study the example tests.

  19. Replace all the auto-generated code in RopeTest by pasting the source code from the example tests into the editor window.

  20. Click the "Compile" button.  The tests should compile without error.

  21. 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.

  22. 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.

  23. Prerequisite: The instructor must have created an account for you on Web-Cat.
  24. Login to Web-CAT, being sure to select "Cal Poly" as the institution. Submit the zip file to Web-CAT for assignment "JUnit Intro".

  25. Study the Web-CAT results online.

  26. 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%.

  27. 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.

  28. 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

  29. 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)