JUnit with the Netbeans IDE


Configure Netbeans to use JUnit

Netbeans already includes the junit classes. So there is nothing to install.

Creating and Using Test Classes

When creating a project, Netbeans assumes you will have the source .java files in one folder and test class .java files in another folder. E.g. The project folder will generally contain two subfolders src and test.

Netbeans assumes the junit library should be used when compiling and running the test class, but it is not included in libraries used for the files in src.

The Projects tree will contain two nodes for libraries:

[+] Libraries
[+] Test Libraries

If you manage to put the test class in the src file along with the class to be tested instead of the in the Test Packages folder (tests), you will need to add the junit library to the Libraries node.

  1. You can copy .java files into the project src or test folder, Netbeans will automatically add them to the project. However, you may need to close and reopen the project or otherwise refresh it to get Netbeans to display the file in the Package navigator window.
  2. The testing class should:
    1. import junit.framework.*;
    2. The convention is to name the testing class the same as the class being tested but with the suffix "Test" added. E.g., if the class being tested is Blob, the testing class would be named BlobTest (in file BlobTest.java).
    3. The testing class should extend the junit class TestCase:
      public class BlobTest extends TestCase
      
    4. When using generic editors like notepad and TextPad, you will need to add a main method to the testing class (e.g. to BlobTest).

      Junit.3.8.1 (but not later version 4) contains two different ways to display output: one is just text output, the other is displayed in a graphical user interface.

      The main method calls one of two methods depending on whether the text output or the graphical user interface output is desired.

      
      (Replace BlobTest by the actual name of the testing class.)
      
      
      
         public static void main(String[] args)
         {
           junit.textui.TestRunner.run( BlobTest.class );
         }
      
      
      
         public static void main(String[] args)
         {
           junit.swingui.TestRunner.run(BlobTest.class);
         }
      
      
    5. For convenience of not having to repeat the same variable declarations and initializations in several different test methods, declare these variables as class data members in the testing class.
    6. Write a routine whose name must be setUp. This method will be executed automatically before each test method is executed. It serves to (re)initialize common variables to their initial state before each test.
    7. Write test methods. The name of each test method should have the prefix "test".
    8. Each test method should have no arguments and have void return type.
    9. In constructing each test, you generally provide an expression related to the methods in the class to be tested and a value that you expect that expression to be equal to.
    10. The JUnit class TestCase has these methods which are inherited by the testing class and can be used to construct tests:

      assertEquals(expected_value, expression)

      assertTrue(boolean_expression)

      assertFalse(boolean_expresion)

      assertNull(expression)

      assertNotNull(expression)

      fail()

      If an assertXXX method fails in a test method, that test method fails.

      If the fail() method is executed, it always fails. It is typically used conditionally. That is, you could write an if statement with one alternative calling fail(). This would be appropriate if that alternative should never happen. A common use is with a try .. catch construct. The catch block is only executed if an exception occurs while the try block is executed completely if no execption occurs.

      If some code should throw an exception, you can test it by puting that code in a try block and put a call to fail() also in the try block just after the code. The catch block can have an empty body. If the fail() method is reached, the code did not throw an exception as it should have and the test method fails!

      If a test method fails, the remaining test methods are still executed and the output reports for each method whether it succeeded or failed.

      If a test failed, further information is given. E.g. if a call to assertEquals fails the expected value and the actual value of the expression are output.

Running the JUnit tests
  1. Netbeans recognizes JUnit classes and the Run menu should have a Test ".." selection that will run the JUnit test methods in your test class and display the results in a JUnit window.