Creating a JUnit Test
See Also
The JUnit test generator allows 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.
- Generated tests are distinguished by appending Test to
the tested classes' names (e.g. MyClassTest.java).
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 contextual menu.
- 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 Empty
Test in the File Types pane. Click Next.
- Enter the Test Name and select any options you require. Click Finish.
The test skeleton is created based on the
properties specified in JUnit
Module Settings.
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.
- Optional Code. Specifies whether to generate the following:
- setUp method. Method for tasks like initializing fields,
enabling on logging, and resetting environment variables.
- tearDown method. Method for cleaning up after tests have
finished running.
- Default Method Bodies. Instructions for printing out information about
test status to the command output.
- Optional 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