IntelliJ IDEA 2024.2 Help

Prepare for testing

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

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.

For more information about testing your code in other languages, refer to:

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 yet added to your project, 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. In the main menu, go to File | Project Structure (Ctrl+Alt+Shift+S).

  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.9.1.

  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.

Add libraries using the Dependencies tool window

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 Maven Artifact Search tool window, type the name of the required dependency, for example: org.junit.jupiter:junit-jupiter. In the list of results, select the one you need and click Add.

    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 about working with Maven, refer to Maven dependencies.

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

  1. In your build.gradle, press Alt+Insert and select Add Maven artifact dependency.

  2. In the Maven Artifact Search tool window, type the name of the required dependency, for example: org.junit.jupiter:junit-jupiter. In the list of results, select the one you need and click Add.

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

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 Test Root 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:

  1. In the Project tool window (Alt+1), create a new directory in which you will store your test code.

  2. Right-click the new directory 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 the Project tool window (Alt+1), create a new directory in which you will store your test code.

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

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

    <build> <testSourceDirectory>src/new-test/test</testSourceDirectory> </build>
  3. 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. In the Project tool window (Alt+1), create a new directory in which you will store your test code.

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

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

    sourceSets { test { java { srcDirs = ['src/new-test/test'] } } }
  3. 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. In the Project tool window (Alt+1), create a new directory in which you will store your test code.

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

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

    sourceSets { test { java { srcDir 'src/new-test/test' } } }
  3. 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 about 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. In the main menu, go to File | Project Structure (Ctrl+Alt+Shift+S).

  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 Structure dialog
Last modified: 11 October 2024