Creating a JUnit Test
See Also
The JUnit test generator enables you to create 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
JUnit Tests (Ctrl-Shift-U).
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
JUnit Tests (Ctrl-Shift-U) from the popup menu.
- Select the JUnit version.
- Make sure the Generate Test Suites checkbox is selected, specify any further
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.
To create an empty test:
- Choose File > New File from the main menu.
- In the New File wizard, select JUnit in the Categories pane and JUnit
Test in the File Types pane. Click Next.
- Specify the test class name, folder and package and select any options you require. Click Finish.
The test skeleton is created based on the
properties specified in JUnit
Module Settings.
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 JUnit 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.
JUnit Test Creation Options
When you generate JUnit tests from existing classes, you can specify the
following options:
- Method Access Levels. Specifies which methods to generate test
methods for.
- Class Types. Specifies which class types to generate test classes
for. This option is only available when generating tests for multiple classes.
- Optional Classes. Specifies whether to generate a test suite or
whether to just generate the individual test classes. This option is only
available when generating tests for multiple classes.
- Generated Code. Specifies whether to generate the following:
- Test Initializer.. Method for tasks like initializing fields,
enabling on logging, and resetting environment variables.
- Test Finalizer. Method for cleaning up after tests have
finished running.
- Default Method Bodies. Instructions for printing out information about
test status to the command output.
- Generated Comments. Specifies whether to create Javadoc comments
for the test methods and comments that suggest how to implement the test
methods.
- See Also
- Running a JUnit Test
- Configuring JUnit
- About JUnit
Legal Notices