IntelliJ IDEA 2021.3 Help

Prepare for testing

IntelliJ IDEA works with multiple testing frameworks out of the box, for example, JUnit, TestNG, Cucumber, or Arquillian JUnit.

In the IDE, you can create a test class directly from the source code together with the necessary test methods. You can switch between test classes and source code with a shortcut, run multiple tests, view statistics for each test, and export test results to a file.

IntelliJ IDEA also features code coverage that allows you to analyze your code and understand which areas of your code are covered by tests and which areas require more testing.

Test results shown on the Test Runner tab of the Run tool window

Add testing libraries

IntelliJ IDEA allows you to add missing libraries as you code: once the IDE detects that you're using some code from the library that is not added to your project yet, it will prompt you to download and install it.

Adding a missing library via quick-fix

You can also add libraries to your project manually. For example, this can be helpful if you need a specific library version or distribution.

Manually add a testing library

Follow these steps to add a library if you're building your project with the native IntelliJ IDEA builder:

  1. From the main menu, select File | Project Structure (Ctrl+Alt+Shift+S) or click the Project Structure button on the toolbar.

  2. Under Project Settings, select Libraries and click the New Project Library button | From Maven.

  3. In the dialog that opens, specify the necessary library artifact, for example: org.junit.jupiter:junit-jupiter:5.4.2 or org.testng:testng:6.14.3.

  4. Apply the changes and close the dialog.

    Manually adding a testing library to a project

IntelliJ IDEA allows you to add missing libraries as you code: once the IDE detects that you're using some code from the library that is not added to your project yet, it will prompt you to download it. In this case, the IDE automatically adds the necessary dependencies to your pom.xml.

Adding a missing library via quick-fix

You can also add libraries to your project manually. For example, this can be helpful if you need a specific library version or distribution.

Manually add a testing library

Follow these steps if you're using Maven in your project:

  1. In your pom.xml, press Alt+Insert and select Dependency.

  2. In the dialog that opens, type the necessary artifact. For example, type org.junit.jupiter:junit-jupiter:5.4.2 to add the JUnit library.

    Click Add. IntelliJ IDEA adds the specified dependency to your pom.xml.

    Adding a Maven dependency
  3. When the dependency is added to pom.xml, press Ctrl+Shift+O or click Reimport All Maven Projects in the Maven tool window to import the changes.

For more information on how to work with Maven, refer to Maven dependencies.

In Gradle projects, add the necessary dependencies to your build file manually:

  1. Open your build.gradle and add a dependency to the necessary testing library.

    For example, if you want to use JUnit 5, add the unit test platform:

    test { useJUnitPlatform() }

    Then, add the dependency to the testing library:

    dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' }
  2. When the dependencies are added to your build.gradle, press Ctrl+Shift+O or click Reimport All Gradle Projects in the Gradle tool window to import the changes.

For more information on how to work with Gradle, refer to Gradle projects.

Test Sources Root

Before you start creating tests, make sure that the Test Sources Root is configured for your project. The Test Sources Root is a folder that stores your test code. In the Project tool window, this folder is marked with the the Tests root icon icon.

The IDE processes the code from different sources differently. For example, compilation results for sources and test sources are normally placed into different folders. That is why, if the test sources root is missing, you need to create one. Otherwise, your code might be processed incorrectly.

Create a test root for your project

Follow these steps if you're building your project with the native IntelliJ IDEA builder:

  • In the Project tool window (Alt+1), right-click the directory in which you want to store your test code and select Mark Directory As | Test Sources Root.

    The tests folder should be marked with the the Tests root icon icon.

    Creating the test sources root

Maven uses a standard directory layout for applications. Generally, it's recommended that you conform to this layout in your projects. For example, when you create a test folder in IntelliJ IDEA for a Maven project, the IDE suggests setting the standard name and location for such a folder. In this case, the IDE is also already aware that this test folder is your Test Sources Root.

However, you can override the standard directory layout by modifying the build file.

Change the test root

  1. In your pom.xml, change the testSourceDirectory element.

    Replace src/new-test/test with the path to the folder that you want to use as a test root.

    <build> <testSourceDirectory>src/new-test/test</testSourceDirectory> </build>
  2. Press Ctrl+Shift+O or click Reimport All Maven Projects in the Maven tool window to import the changes.

The new test root should be marked with the the Tests root icon icon in the Project tool window.

Just like Maven, Gradle also has a strict project directory layout. When you create a test folder in IntelliJ IDEA for a Gradle project, the IDE suggests setting the standard name and location for such a folder. In this case, the IDE is also already aware that this test folder is your Test Sources Root.

However, you can override the standard directory layout by modifying the build file.

Use another test root

  1. Open your build.gradle and add the following code.

    Replace src/new-test/test with the path to the folder that you want to use as a test root.

    sourceSets { test { java { srcDirs = ['src/new-test/test'] } } }
  2. Press Ctrl+Shift+O or click Reimport All Gradle Projects in the Gradle tool window to import the changes.

Add one more test root

  1. Open your build.gradle and add the following code.

    Replace src/new-test/test with the path to the folder that you want to use as a test root.

    sourceSets { test { java { srcDir 'src/new-test/test' } } }
  2. Press Ctrl+Shift+O or click Reimport All Gradle Projects in the Gradle tool window to import the changes.

The new test root should be marked with the the Tests root icon icon in the Project tool window.

For more information on different types of folders, refer to Folder categories.

Test Resources Root

Test Resources Root is a folder that stores files associated with your test sources. In the Project tool window, this folder is located in the test root and is marked with Test Resources Root icon.

For Maven and Gradle projects, the test resources folder is usually created automatically. If you're building your project with the native IntelliJ IDEA builder, you might need to create the resource root manually.

Configure the folder for test resources

  1. From the main menu, select File | Project Structure (Ctrl+Alt+Shift+S) or click the Project Structure button on the toolbar.

  2. Under Project Settings, click Modules and then open the Sources tab on the right.

  3. Right-click the test folder and select New Folder. Name the folder resources.

  4. Right-click the new folder and select Test Resources. The folder will be marked with the Test Resources Root icon icon.

  5. Apply the changes and close the dialog.

    Test Resources Root created in the Project Strucuture dilaog
Last modified: 01 August 2022