Creating JUnit tests

To develop JUnit tests follow these steps to set-up your project:
  1. Create a Java project
  2. Open the project's build path property page and add the junit.jar contained in the org.junit plugin of your Eclipse install as an external JAR.
  3. If you want to avoid absolute pathnames on the build classpath you can introduce a class path variable and reference the junit.jar relative to this variable.
  4. If you want to browse the JUnit source then attach the src.jar contained in the org.junit plugin to the junit.jar.
  5. Implement a TestCase subclass with some tests and build them.

You can also use the TestCase wizard:

  1. Open the new wizard dialog by doing one of the following:
  2. Select Java > Junit in the left pane and TestCase in the right pane
  3. Press Next
  4. TestCase creation wizard page 1

  5. In the Source Folder field, enter the name of the source folder in which your test will be created. You can use the Browse button and select the source folder from the resulting dialog.
  6. In the Package field, enter the name (empty name denotes the default package) of the package in which your test will be created. You can use the Browse button and select the package from the resulting dialog.
  7. In the Test case field, enter the name for the test you are creating.
  8. In the Test class, enter (optionally) the name of the class that your test will be testing. Selecting a class for testing will later allow you to create test methods for public method in the selected class.
  9. In the Superclass field, enter the name of the superclass of your test (this class must implement the junit.framework.Test interface).
  10. Use the checkboxes to select the methods that you want to create in the test.
  11. If you left the Test class field empty, then press Finish to create the test.
  12. If you entered the name of the class to be tested, then:

  13. TestCase creation wizard page 2

  14. Use the checkbox tree control to select the methods for which you want the wizard to create tests
  15. Select the Create tasks for generated Test methods if you want to add tasks that will remind you to implement the test methods
  16. Press Finish

Related tasks

Using JUnit

Related reference

JUnit preference page

Copyright IBM Corporation 2000, 2002. All Rights Reserved.