JUnit with the Eclipse IDE


Configure Eclipse to use JUnit

Eclipse already includes the junit classes. In fact it likely has both junit3.8.xxx and also junit4. I will exclusively use junit4. The functionality of junit4 is essentially an extension of junit3.8.1, but with noticeable changes in syntax since the junit4 implementation takes advantage of java's annotation feature.

  1. When creating a project, at the Java Settings dialog, click on the Libraries tab and then the Add Library button.

    A dialog with a list of libraries will appear. Choose Junit.

    You then get a chance to select the version (3.8.xxx or 4). As noted earlier, I would strongly suggest selecting version 4. Then click Finish.

    Eclipse will

  2. Eclipse discourages using default packages, but will permit the package to be left blank (resulting in the default package). If desired you can easily change from the default package to a named package later by creating a new package and dragging the files from the default package into the new package.

    For large projects it is a common practice to put source files in different folders. E.g., .java source files might go in a src folder; .java test files might go in a tests folder; and generate .class files might go in a bin folder.

    To use separate folders, when creating a project, under Project Layout on the Create Java Project dialog, you will need to check one of these:

    o Use project folder as root for sources and class filse
    o Create separate source and output folders
    

    It is slightly easier to put the .java files for the class being tested and the testing class in the same directory until you are comfortable with using separate folders for a project.

  3. Also on the Create Java Project dialog make sure the JRE (Java Runtime Environment) is at least Java 5. Eclipse will have several choices if you more than one version of Java installed.
  4. You can copy .java files into the project by dragging the file from the window explorer into the Eclipse package navigator window. Alternatively if you download or extract a .java file directly into the project folder, Eclipse will automatically add it to the project. However, you may need to right click the package (folder) and click Refresh from the pop up menu to get Eclipse to display the file in the Package navigator window.
  5. 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. Eclipse recognizes JUnit classes and if you first select the .java for a test class, the Run As item will display:
    Java Application
    JUnit Test
    

    You can select either one.

    If you select Java Application, the main routine of the test class will be executed and the results will be displayed according to the main routine:

    If you select JUnit Test, Eclipse will execute is own main routine and display the results in the JUnit window.