Creating a Unit Test
See Also
The unit test generator enables you to create unit test suites and compilable test classes
for use as skeletons in your unit tests. You can create unit tests for single
classes and entire packages, as well as empty test skeletons to be used with
sources you create later.
Notes:
- Generated tests are distinguished by appending Test to
the tested classes' names (e.g. MyClassTest.java).
- To create a test, the project must have a test directory for the tests.
The IDE creates a Test Packages directory by default when you create a project.
If no test directory exists in your project or you want to specify a different directory for your test,
you can specify a test directory in the project properties dialog.
To create a test for a single class:
- Right-click the class in the Projects window and choose Tools > Create
Tests (Ctrl-Shift-U) to open the Create Tests dialog box.
- Select a unit test framework.
- Select the desired code generation options and click OK.
To create a test suite:
- Right-click the package node containing the source files for which you want
to generate the test suite in the Projects window and choose Tools > Create
Tests (Ctrl-Shift-U) from the popup menu.
- Select a unit test framework.
- Select the Generate Test Suites checkbox.
- Select the desired code generation options and click OK.
The IDE generates test classes for all enclosed classes and stores them
in the project's Test Packages node. If any of the test classes already
exist, those classes are updated.
By default the IDE includes all the test classes as part of the
generated test suite.
- You can also create test suites by using the New File wizard
and selecting Test Suite in the the JUnit category or the TestNG category.
To create an empty test:
- Choose File > New File from the main menu.
- In the New File wizard, select JUnit or TestNG in the Categories pane and JUnit
Test or TestNG Test Case in the File Types pane. Click Next.
- Specify the test class name, folder and package.
- Select the desired code generation options and click Finish.
To specify a test directory:
- Right-click the project node in the Projects window and choose Properties.
- In the Properties window, select Sources in the Categories pane.
- Define the properties of the test packages folder in the Test Package Folders list.
You can add or remove the folders that are used for test packages and modify the names of the test packages folder
as it appears in the Projects window. Click Close.
- Your project must have a test package folder to generate unit tests.
If the test packages folder for your project is missing or unavailable, you can create a new folder in your project and
then designate the new folder as the test packages folder in the project's Properties window.
- See Also
- Running a Unit Test
- About Unit Testing
Legal Notices