BlueJ IDE and Unit Testing
CPE 103/305 Lab Activity


Goal: Use the BlueJ IDE to edit, compile, and unit test your Java programs.


  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 "Project" menu select "New Project". (You may name it "Lab2".)

  4. Click the "New Class" button to create a new Java class named Climber.

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

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

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

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

  9. Compile your completed class and correct any errors.

  10. Create an @author tag for each student.

  11. Close the editor window.

  12. From the "Tools" menu, select Preferences. Click the "Interface" tab. Check "Show unit testing tools." Click "OK".

  13. Right-click on the Climber class in the diagram and select "Create Test Class".  Note that a new class is created named ClimberTest.

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

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

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

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

  18. 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 "Doug" to "Dave" and the test should pass.

  19. Create a zip file containing the .java files for 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 "Compress". Choose ".zip" in the combo box. Be sure the JUnit class name ends in "Test" or Web-CAT won't realize it's a JUnit class.

  20. Prerequisite: The instructor must have created an account for you on Web-Cat. You should receive a username and password via email.
  21. Login to Web-CAT, being sure to select "Cal Poly" as the institution. 

  22. Change your password:  click on your name at the top of the screen, then fill in your new password in the form and click "Save".

  23. Change your timezone: On your profile page, scroll down to "Time Formats" and in the "Time zone" selection box choose "Los Angeles".

  24. Submit the zip file you created to Web-CAT for assignment "Climber Testing".

  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 that "Problem Coverage" is less than 100%, and there's a tip about which test case failed. You'll need to correct errors in your code.

  27. In addition, you'll need to improve your tests, so they thorougly exercise the code. This is shown as "Code Coverage". Correct any errors and resubmit your work until it the grader gives it a perfect score (100% in all 3 categories). Your code must run correctly and your tests must demonstrate complete statement coverage.

  28. If you are new to the BlueJ IDE, do a fast read of The BlueJ Tutorial(pdf).

References:
BlueJ and Web-CAT FAQ
Junit style and FAQ
Unit Testing Tutorial
Junit and BlueJ (Video)

JUnit 3 vs JUnit 4

You may encounter two different versions of JUnit: version 3 and version 4.

By default, BlueJ 3.1 uses JUnit 4.

The differences:
Junit 3 Junit 4
  Method names must start with "test"


  No imports needed

 Test class extends junit.framework.TestCase

 assertNotEquals() is not available
Method names can be whatever you want,
but you must precede them with "@Test" annotation.

Must import org.junit packages.

Test class doesn't have to extend anything

assertNotEquals() is available

Personally, I prefer JUnit 3, because it's annoying to me to have to include extra statements that JUnit 4 requires. I often forget them, and the result causes no error messages, and the tests always pass, so I get fooled into thinking everything is fine.

It's easy to setup BlueJ 3.1 to use the older JUnit 3 by default:

Replace this file
/usr/share/bluej/english/templates/newclass/unittest.tmpl
with this one.
(The exact directory path may be different, depending on your platform.)

For more info, consult the BlueJ Reference Manual section 9.28 on templates.

If you are using a CSL lab machine, you aren't allowed to modify the installation templates.
However, you can specify an alternate directory; see the Reference Manual.

In the worst case, just copy-paste this skeleton test class.