Perform unit testing using JUnit

Writing Tests

Before you can write JUnit tests you have to add the junit.jar library to your build class path. The Eclipse installation includes JUnit in the org.junit plug-in:

  1. Open the project's build path property page (on the project's context menu choose Properties > Java Build Path)

  2. Switch to the Libraries tab, click Add Variable for adding the JUNIT_HOME variable that refers to org.junit plug-in directory.

  3. The JUNIT_HOME variable resolves to a folder, you should specify a path extension that points to junit.jar JAR. To do this press the Extend... button.

    Extend

  4. Add the junit.jar contained in JUNIT_HOME directory as a classpath variable extension.

    classpath variable extension

junit.jar

Now that the Java project has access to the JUnit classes you can write your first test. You implement the test in a subclass of TestCase. You can do so either using the standard Class wizard or the specialized Test Case wizard:

  1. Open the New JUnit Test Case wizard (File > New > JUnit Test Case).

  2. Enter "TestFailure" as the name of your test class, type "by.boot.java.test" as the package name:

    JUnit Test Case

  3. Click Finish to create the test class:

    JUnit Test Case class

Add a test method that fails to the class TestFailure. A quick way to enter a test method is with the test template. To do so type "test" followed by Ctrl + Space to activate code assist and select the "test" template. Change the name of the created method to testFailure and invoke the fail() method. Also change the visibility modifier such that the test method is public.

public void testFailure() {
    fail();
} 
					

Now you are ready to run the test.

Running Tests

To run TestFailure, activate the Run drop-down menu in the toolbar and select Run As > JUnit Test:

Run JUnit Test

You can inspect the test results in the JUnit view. This view shows you the test run progress and status:

JUnit view

The view is shown in the current perspective whenever you start a test run. A convenient arrangement for the JUnit view is to dock it as a fast view. The JUnit view has two tabs: one shows you a list of failures and the other shows you the full test suite as a tree.

Test suite as a tree

You can navigate from a failure to the corresponding source by double clicking the corresponding line in the Failure Trace.

Dock the JUnit view as a fast view, remove the fail() statement in the method testFailure() so that the test passes and rerun the test again. You can rerun a test either by clicking the Rerun Last Test button in the view's tool bar or you can re-run the program that was last launched by activating the Run drop down. This time the test should succeed. Because the test was successful, the JUnit view doesn't pop up, but the success indicator shows on the JUnit view icon and the status line shows the test result. As a reminder to rerun your tests the view icon is decorated by a "*" whenever you change the workspace contents after the run.

Test succeeded

In addition to running a test case as described above you can also:

Customizing a Test Configuration

When you want to pass parameters or customize the settings for a test run you open the Launch Configuration Dialog. Select Run... in the Run drop-down menu in the toolbar:

Launch Configuration Dialog - JUnit

In this dialog you can specify the test to be run, its arguments, its run-time class path, and the Java run-time environment.

Debugging a Test Failure

In the case of a test failure you can follow these steps to debug it:

  1. Double click the failure entry from the stack trace in the JUnit view to open the corresponding file in the editor.

  2. Set a breakpoint at the beginning of the test method.

  3. Select the test case and execute Debug As > JUnit Test from the Debug drop down.

A JUnit launch configuration has a "keep alive" option. If your Java virtual machine supports "hot code replacement" you can fix the code and rerun the test without restarting the full test run. To enable this option select the Keep JUnit running after a test run when debugging checkbox in the JUnit launch configuration.

Creating a Test Suite

The JUnit Test Suite wizard helps you with the creation of a test suite. You can select the set of classes that should belong to a suite.

  1. Open the New JUnit Test Suite wizard :

    Select Java > JUnit > JUnit Test Suite and click Next.

  2. Enter a name for your test suite class (the convention is to use "AllTests" which appears by default). Click Browse to select "by.boot.java.test" package.

  3. Select the classes that should be included in the suite. We currently have a single test class only, but you can add to the suite later.

    New JUnit Test Suite

You can add or remove test classes from the test suite in two ways:

NOTE: the wizard puts 2 markers, //$JUnit-BEGIN$ and //$JUnit-END$, into the created Test Suite class, which allows the wizard to update existing test suite classes. Editing code between the markers is not recommended.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide