In this tutorial we will learn how to create a JUnit 5 project using IntelliJ. We will be at first creating a simple IntelliJ Project, then we will show how to create a JUnit 5 project with Maven.

Creating a JUnit IntelliJ Project:

Requirements: Download IntelliJ Community edition from: https://www.jetbrains.com/idea/download/

Install IntelliJ Idea, then launch it:

Step 1: Create a new Java Project:

junit tutorial intellij maven junit tutorial intellij maven

Step 2: Choose if you want to create an Hello World Template (not required for this tutorial)

junit tutorial intellij maven junit tutorial intellij maven

Step 3: Choose a Project name:

junit tutorial intellij maven junit tutorial intellij maven

Step 4: Choose a Folder for your project:

junit tutorial intellij maven junit tutorial intellij maven

Step 5: Add a new Java class to your project:

junit tutorial intellij maven junit tutorial intellij maven

Here is our basic class:

public class UnitTestDemo {

    public int sum(int x, int y) {
        return x+y;
    }
}

Next, choose to Generate a Test for it:

junit tutorial intellij maven junit tutorial intellij maven

Enter the class Name and click on Fix in order to let IntelliJ add JUnit library to the project:

junit tutorial intellij maven junit tutorial intellij maven

By Clicking on Fix you can choose where to install the JUnit library:

 junit tutorial intellij maven junit tutorial intellij maven

Now that the environment is ready, let's update the class generated with the following Test:

import org.junit.jupiter.api.Test;



class UnitTestDemoTest {
    @Test
    public void testSum() {
        UnitTestDemo test = new UnitTestDemo();
        assert(test.sum(3,3) == 6);
    }

}

Here is how your JUnit project should look like:

junit tutorial intellij maven junit tutorial intellij maven

Now right-click on the Test class and check that the Test passed:

junit tutorial intellij maven junit tutorial intellij maven

Creating a JUnit Maven Project with IntelliJ:

The steps to create a JUnit project with Maven are quite similar to the previous example, except that you will create a new Maven project instead:

junit tutorial intellij maven junit tutorial intellij maven

Choose the maven-quickstart archetype which provides a bare bones example of Java project with a Test class.

Specify the GroupId and ArtifactId:

junit tutorial intellij maven junit tutorial intellij maven

Specify the Project location:

junit tutorial intellij maven junit tutorial intellij maven

Here is the Project Object Model (pom.xml) that has been created:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.junit5</groupId>
    <artifactId>running-unit-tests</artifactId>
    <version>0.1</version>
    <name>Running Unit Tests</name>
    <description>
        This example demonstrates how you can run unit tests which use JUnit 5.
    </description>

    <properties>
        <jdk.version>1.8</jdk.version>
        <junit.jupiter.version>5.5.2</junit.jupiter.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.jupiter.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>running-unit-tests</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
</project>

Now let IntelliJ load the dependencies so that the project can be run from within IntelliJ:

junit tutorial intellij maven junit tutorial intellij maven

Now edit the App.java class as follows:

package com.example;

public class App 
{
    public int sum(int x, int y) {
        return x+y;
    }

    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

And edit the AppTest class as follows:

package com.example;

import org.junit.Test;

public class AppTest 
{
 
    @Test
    public void shouldAnswerWithTrue()
    {

        App test = new App();
        assert(test.sum(3,3) == 6);
    }
}

Here is how your jUnit Maven project should look like:

junit tutorial intellij maven junit tutorial intellij maven

Now you can execute as you already did it for the basic IntelliJ Project.

Congratulations! You just managed to create your first JUnit Project with IntelliJ Idea!

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials