IntelliJ IDEA
 
Get IntelliJ IDEA

Get started with JUnit

Last modified: 24 October 2024

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, Gradle, or the IntelliJ builder.

Create a project

  1. In the main menu, go to File | New | Project.

  2. In the New Project wizard, select Java from the list on the left.

  3. Specify the name for the project, for example, junit-tutorial, and select Maven as a build tool.

  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 about 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 CtrlShift0N and enter its name.

  2. In pom.xml, press AltInsert and select Dependency.

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

    Locate the necessary dependency in the search results and click Add.

  4. When the dependency is added to pom.xml, press CtrlShift0O or click Reimport All Maven Projects in the Maven tool window to import the changes.