blob: c7ec54813cc755abcc3efa1daa131e8fc647e62c [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2007. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title>
Writing and running JUnit tests
</title>
</head>
<body>
<h1>
Writing and running JUnit tests
</h1>
<p>
In this section, you will be using the <a href="http://www.junit.org" target="_blank"> JUnit</a>
testing framework to write and run tests. To get started with JUnit you can
refer to the <a href="http://junit.sourceforge.net/doc/cookbook/cookbook.htm" target="_blank">JUnit
Cookbook</a>.
</p>
<h2>
Writing Tests
</h2>
<p>
Create a project &quot;JUnitTest&quot;. Now you can write your
first test. You implement the test in a subclass of <strong>TestCase</strong>. You can
do so either using the standard Class wizard or the specialized <strong>Test Case
</strong> wizard:
</p>
<ol>
<li>Open the New wizard (<strong>File &gt; New &gt; JUnit Test Case</strong>).</li>
<li>Select <strong>New JUnit 3 test</strong> and enter
&quot;<i>TestFailure&quot;</i> as the name of your test class:
<p>
<img src="images/wiz_new_testcase.png" alt="TestCase creation wizard page 1"><br>
</p>
<p>
<em>Note: </em>If you want to use JUnit 4 tests you have to make
sure that your compiler compliance is set to 5.0.
</p>
</li>
<li>You will see a warning message asking you to add the junit library
to the build path. Use the <strong>Click here</strong> link to
add the junit library automatically.</li>
<li>Click <strong>Finish</strong> to create the test class.
</li>
</ol>
<p>
Add a test method that fails to the class <i>TestFailure</i>. A quick
way to enter a test method is with the <i>test</i> template. To do so, place the cursor inside the class declaration.
Type &quot;test&quot; followed by <kbd>Ctrl+Space</kbd> to activate code
assist and select the &quot;test&quot; template. Change the name of the
created method to <i>testFailure</i> and invoke the <i>fail() </i>method.
</p>
<p><code>
public void testFailure() throws Exception {<br>
&nbsp;&nbsp;&nbsp; fail();<br>
}
</code></p>
<p>Now you are ready to run your first test.</p>
<h2>
Running Tests
</h2>
<p>To run TestFailure hit the run button in the toolbar. It will automatically run as JUnit Test.
You can inspect the test results in the <i>JUnit</i> view. This view shows
you the test run progress and status:</p>
<p><img src="images/view_junit_red.png" alt="Failed test"></p>
<p> 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. You can navigate from a failure to the corresponding source by
double clicking the corresponding line in the failure trace.</p>
<p>Dock the JUnit view as a fast view, remove the <i>
fail()</i> statement in the method <i> testFailure()</i> so that the test passes and rerun
the test again. You can rerun a test either by clicking the <b>Rerun</b> button in
the view's tool bar or you can re-run the program that was last launched by activating
the <b>Run</b> 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 &quot;*&quot;
whenever you change the workspace contents after the run.</p>
<blockquote>
<p><img src="images/icon_junit_successfull.png" border="0" alt="Successful test">
- A successful test run<br>
<img src="images/icon_junit_successfull_workspace_modifed.png" border="0" alt="Successful test but workspace has changed"> -
A successful test run, but the workspace contents have changed since the
last test run.</p>
</blockquote>
<p>In addition to running a test case as described above you can also:</p>
<ul>
<li>Run all tests inside a project, source folder, or package - &nbsp;<br>
Select a project, package or source folder and run all the included tests with <strong>Run as &gt; JUnit Test</strong>.
This command finds all tests inside a project, source folder or package
and executes them.</li>
<li>Run a single test method - <br>
Select a test method in the Outline or Package Explorer and with <strong>Run as &gt; JUnit Test</strong>
the selected test method will be run.</li>
<li>Rerun a single test - <br>
Select a test in the JUnit view and execute <b>Run </b>from the context
menu.</li>
</ul>
<h2>
Customizing a Test Configuration
</h2>
<p>When you want to pass parameters or customize the
settings for a test run you open the Launch Configuration Dialog. Select <strong>Open Run Dialog...</strong>.in the <strong>Run</strong>
drop-down menu in the toolbar:</p>
<p><img src="images/run_dialog_junit.png" alt="JUnit Launch Configuration"></p>
<p>In this dialog you can specify the test to be run, its arguments, its run-time
class path, and the Java run-time environment.</p>
<h2>
Debugging a Test Failure
</h2>
<p>In the case of a test failure you can follow these steps to debug it:</p>
<ol>
<li>Double click the failure entry from the Failures tab in the JUnit view to open the
corresponding file in the editor.</li>
<li>Set a breakpoint at the beginning of the test method.</li>
<li>Select the test case and execute&nbsp; <b>Debug As&gt;JUnit Test</b> from
the <b>Debug </b>drop down.</li>
</ol>
<p>A JUnit launch configuration has a &quot;keep alive&quot; option. If your
Java virtual machine supports &quot;hot code replacement&quot; you can fix the code and rerun the test
without restarting the full test run. To enable this option select the <strong>Keep
JUnit running after a test run when debugging</strong> checkbox in the JUnit
launch configuration.</p>
<h2>
Creating a Test Suite
</h2>
<p>
The JUnit <b>TestSuite </b>wizard helps you with the creation of a test
suite. You can select the set of classes that should belong to a suite.
</p>
<ol>
<li>Open the New wizard</li>
<li>Select <strong>Java &gt; JUnit &gt; JUnit Test Suite</strong> and click <strong>Next</strong>.</li>
<li>Enter a name for your test suite class (the convention is to use
&quot;AllTests&quot; which appears by default).<br>
<p><img src="images/wiz_new_suite.png" alt="Test Suite wizard"></p>
</li>
<li>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.</li>
</ol>
<p>You can add or remove test classes from the test suite in two ways:</p>
<ul>
<li>Manually by editing the test suite file</li>
<li>By re-running the wizard and selecting the new set of test classes.</li>
</ul>
<p>Note: the wizard puts 2 markers, <code>//$JUnit-BEGIN$</code> and
<code>//$JUnit-END$</code>, into the created Test suite class, which allows
the wizard to update existing test suite classes. Editing code between the markers is
not recommended.</p>
<P class="nav_footer" id="nav_footer">Next Section: <A href="qs-ProjectConfiguration.htm">Project configuration tutorial</A>
</P>
</body>
</html>