BlueJ IDE and Unit Testing
CPE 103/303 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. Open a terminal window.
  20. Make a remote connection (using ssh) to one of the CSL Unix servers (e.g., unix1.csc.calpoly.edu).  

  21. Navigate to the directory where you compiled the Climber class.

  22. Run the instructor's unit tests with this command:
    ~graderjd/Labs/climbertest

  23. You will probably find that the instructor's tests 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. You'll need to correct errors in your code.

  24. When your code passes the instructor tests you can submit your work.  Submit your source code and unit tests as described in the syllabus.

References:
BlueJ FAQ
Junit style and FAQ
Unit Testing Tutorial
Junit and BlueJ (Video)
The BlueJ Tutorial(pdf).

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.