IntelliJ IDEA 2020.3 Help

Create tests

The simplest way of creating a new test class in IntelliJ IDEA is by using a dedicated intention action that you can invoke from your source code. In this case, the IDE creates a new test class and generates test code for this class, package, or function.

Add a new test

  1. In your production code in the editor, place the caret at the class for which you want to create a test, press Alt+Enter, and select Create Test.

  2. In the Create Test dialog, select the library that you want to use.

    If you don't have the necessary library yet, you will be prompted to download it. To do that, click Fix.

  3. Configure the test class name and its location and select the methods that you want to test. Click OK.

    As a result, IntelliJ IDEA creates a new test class with the specified name and generated test methods in the Test Sources Root.

    Creating a new test using the Create Test context action

Create Test dialog controls

Item Description
Testing library Select the testing framework that you are going to use.

This button is available when a library for the selected testing framework is missing. Click it to download and install the necessary library.

If you're using Maven, the IDE will add the missing dependencies to your pom.xml. For Gradle projects, add the necessary dependencies manually.

Class name Enter the name for the test class or accept the default name. You can change the way test classes are named in the settings.
Superclass For JUnit3, the superclass junit.framework.TestCase is suggested automatically. For the other supported frameworks, this field is blank.
Destination package Specify the name of the package where the generated test class will be stored.




Include stub methods for test fixtures and annotations into the generated test class.
Show inherited methods Select this option to show all methods, including the inherited ones.
Generate test methods for Select the methods for which you want to generate test methods.

Configure naming pattern for test classes

By default, IntelliJ IDEA adds the Test suffix to class names when generating test classes. For example, if you create a test class for HelloWorld, its name by default is HelloWorldTest. You can change the naming pattern in the settings.

  1. In Settings/Preferences Ctrl+Alt+S, go to Editor | Code Style | Java, and open the Code Generation tab.

  2. In the Naming section of the tab, locate the Test fields and type a suffix or a prefix (or both) that you want to use for generated test classes into the corresponding fields.

Navigate between test code and source code

In IntelliJ IDEA, you can jump between the test classes and the source code.

  • In the editor, place the caret at the test class or at the test subject in the source code and press Ctrl+Shift+T (Navigate | Test Subject or Navigate | Test).

    Navigate to test from production code

If there's only one test for this class, the IDE will navigate you to it right away. Otherwise, you will be prompted to select the necessary test from a popup or create a new test.

Last modified: 15 February 2021