How to Download and Install JUnit in Eclipse

โšก Smart Summary

JUnit installation in Eclipse takes six ordered parts: verify the JDK, download the jars, set environment variables, add the jars to the build path, confirm the libraries, and run a first passing test.

  • ๐Ÿ”˜ Prerequisite: Install or verify a Java Development Kit first, because JUnit only tests Java code.
  • โ˜‘๏ธ Download: Two jars are needed for JUnit 4 โ€” junit.jar and hamcrest-core.jar.
  • โœ… Environment: JUNIT_HOME and CLASSPATH matter for command-line runs, not for Eclipse itself.
  • ๐Ÿงช Build path: Add External JARs attaches the downloaded jars to a single Eclipse project.
  • ๐Ÿ› ๏ธ Verification: The Libraries tab lists every jar Eclipse actually resolved for the project.
  • โšก Versions: JUnit 4.13.2 closes the JUnit 4 line; Jupiter artifacts replace it for new work.

How to Download and Install JUnit in Eclipse

Installing JUnit is a six-part process, explained in detail below โ€” from the Java Development Kit on your machine to a first test that turns the Eclipse bar green.

PART 1) Install Java

JUnit is a testing framework used to test Java based applications. So before installing JUnit, you need to configure or verify the Java Development Kit (JDK) on your machine.

Click this tutorial to download and install Java. Any currently supported JDK release works for the steps that follow; the screenshots on this page were captured on JDK 1.7 and JDK 1.8.

PART 2) Download JUnit

Step 1) Visit junit.org/junit4 and click Download and Install. The link sits in the Welcome column of the JUnit home page, as the arrow below shows.

JUnit home page with the Download and install link highlighted in the Welcome column

Step 2) Click junit.jar. The Download and Install link opens the junit4 wiki page on GitHub, and the Plain-old JAR section lists the two jars you need.

junit4 GitHub wiki Download and Install page with junit.jar highlighted under Plain-old JAR

Step 3) In the central repository you are shown all versions of JUnit that can be downloaded. Usually, you will select the latest version. Click on the jar link to download JUnit version 4.12 as shown below.

Maven Central search results for the junit artifact with the jar link on the 4.12 row highlighted

Step 4) Visit the junit4 Download and Install wiki page again. Click hamcrest-core.jar, the second entry in the same list.

Plain-old JAR section of the junit4 wiki with hamcrest-core.jar circled

Step 5) Download the jar. The hamcrest-core entry opens in the repository, where the jar link starts the download.

Maven Central search results for org.hamcrest hamcrest-core version 1.3 with the jar download link highlighted

For JUnit installation, you need the JUnit jars, and you can download the desired version of the JUnit jar file from the JUnit official website junit.org.

Here is the jars list:

  • junit.jar
  • hamcrest-core.jar

Version note: the screenshots show JUnit 4.12. JUnit 4 is now in maintenance mode with 4.13.2 as its final release, so the wiki today points junit.jar at 4.13.2 and hamcrest-core.jar at 1.3. New projects use the Jupiter artifact org.junit.jupiter:junit-jupiter instead, which needs no hamcrest jar. Only the file name changes; the steps below do not. See the JUnit test framework tutorial.

PART 3) JUnit Environment Setup

Step 1) You need to set the JUNIT_HOME environment variable to point to the base location where you have placed the JUnit jars.

For example, if you have created a JUnit folder on the C: drive and placed the jars there, then for environment settings you need to open Control Panel -> Advanced -> Environment Variables, and click the New button under System variables.

Windows Environment Variables dialog listing JAVA_HOME, JUNIT_HOME and MAVEN_HOME with the New button highlighted

When you click the New button in Environment Variables, it opens another window.

Step 2) A “New System Variable” window will open:

  1. Provide the variable name as “JUNIT_HOME”.
  2. Provide the JUnit value as the JUnit path where you have copied the JUnit jar files.
  3. Click on OK.

In the capture below the value is the folder C:\Junit4, which is where this walkthrough saved the jars.

New System Variable dialog with JUNIT_HOME entered as the variable name and C:\Junit4 as the value

When you click OK, it will create a new system variable with the given name and value, which you can verify in the Environment Variables window shown in Step 1.

Step 3) After creating JUNIT_HOME, create another variable with the name CLASSPATH. Again go to Environment Variables and click the “New” button, which opens the same New System Variable window.

Environment Variables dialog reopened to add a second system variable for CLASSPATH

Step 4) In this step, point CLASSPATH at the jar placed in the JUnit folder as given below:

  1. Variable Name: CLASSPATH
  2. Variable Value: %CLASSPATH%;%JUNIT_HOME%\junit-4.10.jar;.;
  3. Click on the OK button.

Substitute the exact file name you downloaded: the build path in Part 4 shows this machine using junit-4.10.jar.

New System Variable dialog with CLASSPATH as the variable name and a value referencing %JUNIT_HOME%

Step 5) Once you click the ‘OK’ button, you can verify that a new environment variable named “CLASSPATH” can be seen under System variables. See below.

Environment Variables dialog showing the new CLASSPATH entry selected in the System variables list

Note: these two variables only matter when you compile and run tests from the command line. Eclipse ignores the system CLASSPATH and resolves jars from the project build path, which Part 4 configures. Maven and Gradle projects need neither, because the build tool assembles the test classpath for you.

PART 4) Install the JUnit Jar File in Eclipse

Step 1) Right click on the project:

  1. Click on “Build Path” and then
  2. Click on “Configure Build Path”.

The Build Path submenu is where every classpath change on a project begins.

Eclipse project context menu with the Build Path submenu open and Configure Build Path highlighted

Step 2) In this step,

  1. Go to the Java Build Path window as shown in the figure below
  2. Now click on the “Add External JARs” button to add your downloaded JUnit.jar file to Eclipse.

After adding the JUnit jar file, click the ‘OK’ button to close the Java Build Path window.

Java Build Path Libraries tab in the project Properties dialog with the Add External JARs button highlighted

Shortcut: the Add Libraryโ€ฆ button in the same panel attaches the JUnit library bundled with Eclipse, so a project can reach JUnit 4 or JUnit 5 without any manual download. The manual route still matters when you need a specific version or work offline. Next, write tests with JUnit annotations.

PART 5) Verify the JUnit Jar File in Your Build Path

In order to verify the JUnit jar file in Eclipse, you need to follow the steps mentioned below:

  1. Right click on the project -> Build Path
  2. Click on “Configure Build Path”.

This is the same menu used in Part 4, reached from the editor this time.

Eclipse context menu opened over an editor with Build Path and Configure Build Path marked as steps one and two

Step 2) The Java Build Path window will appear as shown below.

In that window, go to the Libraries tab to see all jar files. In the jar file tree view, you need to look for the jar file name that starts with JUnit.

Once you expand the JUnit library, you can see the Java libraries as shown below:

Libraries tab tree expanded to show the junit entry containing junit-4.10.jar from C:\Junit4

Now you are ready to use JUnit with Eclipse. If this tree shows no JUnit entry, the jar was never attached and every org.junit import in your test classes will fail to resolve.

PART 6) Verify the JUnit Setup

You can create a simple JUnit test to verify the JUnit setup. See the test class below.

Step 1) Create a Java class named TestJunit.java and provide a simple assert statement.

Eclipse editor showing the TestJunit class with a testSetup method and an assertEquals call

package guru99.junit;
   
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
   @Test
   public void testSetup() {
      String str= "I am done with Junit setup";
      assertEquals("I am done with Junit setup",str);
   }
}

Step 2) Create a Test Runner class to execute the test above.

Eclipse editor showing the TestRunner class with a main method that calls JUnitCore.runClasses

package guru99.junit;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class TestRunner {
   public static void main(String[] args) {
      Result result = JUnitCore.runClasses(TestJunit.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println("Result=="+result.wasSuccessful());
   }
} 

Step 3) To execute the test, follow the steps below:

  1. Right click on TestJunit.java and click on “Run As” as shown below
  2. Another window will open once you click on “Run As”; click on “1 JUnit Test” as shown below:

Eclipse Run As submenu opened over TestJunit.java with the 1 JUnit Test entry highlighted

Step 4) Here is the output or result for your test. If it executes successfully, a green check mark appears in front of it.

JUnit view reporting Runs 1/1 with zero errors, zero failures and a green progress bar for testSetup

The view reports Runs: 1/1, Errors: 0, Failures: 0 with the runner marked JUnit 4, confirming that the jars, the build path and the test class are wired correctly. A red bar here usually means the jars are missing from the build path. See the JUnit assert reference, and how to create a JUnit test suite.

FAQs

JUnit 4.13.2 is the final JUnit 4 release and the line is in maintenance mode. New projects should install the Jupiter artifacts of JUnit 5 or JUnit 6, which run on the JUnit Platform and need no hamcrest jar.

Only for JUnit 4. Its assertThat matchers live in hamcrest, so junit.jar alone throws NoClassDefFoundError. Jupiter dropped that dependency entirely, so a JUnit 5 or JUnit 6 setup never asks for hamcrest-core.jar.

Yes. Eclipse bundles JUnit libraries, so Add Libraryโ€ฆ on the Java Build Path attaches JUnit 4 or JUnit 5 in one step. Eclipse also offers to add the library automatically the first time you create a test case.

Declare the dependency and let the build tool fetch it. Maven uses junit:junit in test scope for JUnit 4, or org.junit.jupiter:junit-jupiter for Jupiter. Gradle uses testImplementation with the same coordinates. No manual jar or environment variable is involved.

The jar is not on that project’s build path. Setting CLASSPATH in Windows does not fix it, because Eclipse resolves types from the build path only. Reopen Configure Build Path and confirm the JUnit entry appears under Libraries.

They help most with diagnosis. Pasting a build path error or a stack trace into an assistant usually identifies a missing jar, a version clash or a wrong package. Always confirm the suggested version against the official release notes before applying it.

Yes. Once the library resolves, it drafts test methods, assertions and setup code from the class under test. Review each suggestion, because it can mix JUnit 4 and Jupiter imports in the same file and quietly break compilation.

No. Those variables serve command-line javac and java runs. Eclipse uses the project build path, and Maven or Gradle build their own test classpath, so many developers never set either variable at all.

Summarize this post with: