Introduction to JUnit Testing with NetBeans


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

  2. From the "File" menu select "New Project".   In the "Choose Project" dialog, select "Java Application" and click "Next". In the "New Java Application" dialog, name your project "ClimberUnitTesting".   Choose the desired location for the project folder and click "Finish."

  3. From the "File" menu select "New File".  In the "New File" dialog, select "Java Class" and click "Next".  In the "New Java Class" dialog, name the class "Climber".  Click "Finish".

  4. Copy and paste the source code from this skeleton file into the editor window.  Notice the errors that appear because the class is incomplete.

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

  6. Compile your completed class and correct any errors.

  7. Put your name in the  @author tag.

  8. Close the editor window.

  9. From the "File" menu select "New File".  In the "New File" dialog, select "Unit Tests" category and JUnit Test file type and click "Next".  In the "New JUnit Test" dialog, name the class "ClimberTest".  Click "Finish".

  10. In the "Select JUnit Version" dialog, please, please, PLEASE select JUnit 3.x  (See below).
    JUnit select dialog

  11. Replace all the auto-generated code for ClimberTest by pasting the source code from these example tests into the editor window.  Save the file.

  12. Compile your completed class and correct any errors.

  13. From the "Run" menu, choose "Test Project".  This will run all the JUnit tests in the project. Observe the results of running the tests.  One of the four tests fails because the test is formulated incorrectly.  A hint gives details about the failed test. To make the correction,  change "Doug" to "Dave" and rerun the test.  The test should pass.

  14. There is a subtle defect in the skeleton code for Climber.java.  Inspect the code to find the defect.  When you think you have discovered it, add a new test case to ClimberTest that will fail, thereby revealing the defect.  

  15. Now repair the defect in Climber.java.   Rerun the tests to see if they pass.  Correct any errors until the code and tests run without error.

  16. Submit printouts of both the source code and unit test code as described in the syllabus.
 

JUnit 3 vs JUnit 4

You may encounter two different versions of JUnit: version 3 and version 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.  This is a really bad scenario that we want to avoid.