IntelliJ IDEA
 
Get IntelliJ IDEA
You are viewing the documentation for an earlier version of IntelliJ IDEA.

JUnit 5

Last modified: 10 August 2022

In this tutorial, you will learn how to set up JUnit for your projects, create tests, and run them to see if your code is operating correctly. It contains just the basic steps to get you started.

If you want to know more about JUnit, refer to the official documentation. To learn more about testing features of IntelliJ IDEA, refer to other topics in this section.

You can choose to follow the tutorial using either Maven or Gradle.

Create a project

  1. From the main menu, select File | New | Project.

  2. Select New Project. Specify the name for the project, for example, junit-tutorial.

  3. Select Maven as a build tool. In Language, select Java.

  4. From the JDK list, select the JDK that you want to use in your project.

    If the JDK is installed on your computer, but not defined in the IDE, select Add JDK and specify the path to the JDK home directory.

    If you don't have the necessary JDK on your computer, select Download JDK.

  5. Click Create.

tip

For more information on working with Maven projects, refer to Maven.

Add dependencies

For our project to use JUnit features, we need to add JUnit as a dependency.

  1. Open pom.xml in the root directory of your project.

    tip

    To quickly navigate to a file, press Ctrl+Shift+N and enter its name.

  2. In pom.xml, press Alt+Insert, select Add dependency.

  3. In the tool window that opens, type org.junit.jupiter:junit-jupiter in the search field.

    Locate the necessary dependency in the search results and click Add next to it.

  4. Now we need to apply the changes in the build script. Press Ctrl+Shift+O or click Load Maven Changes in the notification that appears in the top-right corner of the editor.

    The Load Maven Changes button