JUnit 4 Setup Example with IntelliJ + Maven

In this tutorial, we take a look at how to setup JUnit 4 with IntelliJ IDE and Maven.

Step 1: Create a Java Project in IntelliJ with Maven

Open IntelliJ IDE and click on New Project button, or go to the menu option File -> New -> Project...

Java New Project in IntelliJ IDE

Make sure to select the Build System as Maven.

Build System Maven

Step 2: Adding Junit 4 Maven Dependency to pom.xml

In the project folder, open the pom.xml file.

Add the below junit 4 dependency to your pom.xml file.


Make sure to "Load Maven Changes" so the required jar files get downloaded.

IntelliJ - Load Maven Changes

Step 3: Sample code to verify setup

Create a class TestJunit.java under src -> test -> java

import org.junit.Test;
import static org.junit.Assert.*;

public class TestJunit {

    public void test1() {

        int expectedResult =  20;
        int actualResult = 20; //for testing


    public void test2() {

        int expectedResult =  20;
        int actualResult = 19; //for testing



Now right-click on this class file and select - "Run TestJunit with Coverage", you should see one test pass and another fails.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap