Download, Install And Configure JUnit In Eclipse

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This Tutorial Explains How To Download, Install, and Configure JUnit in Eclipse. It includes Setting up Environment Variables, JUnit 5 Architecture & Setup:

As JUnit is a framework for Java, it needs to be installed before JUnit installation. Also, download eclipse.exe on your system to get Eclipse working prior to setting up the JUnit.

Assuming that Java and Eclipse are in place, we will learn the following in this tutorial:

  1. How to download, install, configure, and get JUnit 4 working on our system.
  2. All about JUnit 5 architecture in brief.
  3. How to install and get JUnit 5 working on our system?
  4. The JDK versions supported by JUnit 4 and JUnit 5.

=> Check Out The Perfect JUnit Training Guide Here.

Download and Install JUnit in Eclipse

Download & Install JUnit In Eclipse

Given below are three prominent steps to have Junit working on our system.

  1. Download the JUnit jars from the said URLs.
  2. Associate these jars to the system variables by setting up the environment variables.
  3. Configure and build the path in Eclipse.

Now, let’s dive into the process to understand the practical approach of setting up JUnit.

Download JUnit jars

#1) Navigate to URL junit.org/junit4. You could type ‘/junit4’ in the URL in case you wish to download a jar file for JUnit version 4. Use ‘/junit5’ in the URL if you wish to download a jar file for JUnit version 5.

Screenshot below:

download jar file

The Download and Install link redirects nowhere but to the Github link where the JUnit jar could be found.

#2) Download the following JARs and add them to your test classpath:

  1. Junit.jar
  2. Hamcrest-core.java

Click here for the alternate URL to download the jars.

Set Up Environment Variables

Now we will look at how to Set up our environment variables to get the JUnit working.

#1) The first step is to go to Environment Variables, On the panel, under the System variables, click on the New button, add a variable JUNIT_HOME, and against it add the path where you have placed the JUnit jars.

(Note: The navigation flow to go to the Environment Variables is Control Panel => Advanced =>Environment Variables)

Environment Variables

#2) On clicking New under System Variables, pop up displays where the first textbox will require the user to enter the variable name i.e. JUNIT_HOME in this case and a second text field that requires the user to enter the path of the JUnit jar files. i.e. C:\lanadmin\junit4 as an instance.

Check the below screenshot for better clarity:

JUNIT_HOME

#3) On clicking OK, an entry of JUNIT_HOME adds to the records of the System Variable section.

#4) With a similar approach as above, next add a new variable name CLASSPATH with value %CLASSPATH%; JUNIT_HOME%\junit jar location which is %CLASSPATH%; JUNIT_HOME%\C:\lanadmin\junit.jar per our instance.

It then gets added to the variable list of the Environment variables as shown below:

Classpath

To run your JUnit tests, you’ll need the following elements in your CLASSPATH:

  • JUnit class files
  • Other Java test class files
  • Libraries your class files depend on. In other words, all the required .jar files.

Configure And Build JUnit In Eclipse

#1) Create a new Project folder.

#2) Right-click on the project folder created, and click on Build Path => Configure build path.

Configure build path

#3) A window titled Java Build Path opens.

#4) Click on the External Jars button which opens up a window that allows you to browse the JUnit jar files or library files. On selecting the path of the jar files, all the library files needed for JUnit get added in the Eclipse.

Screenshot below:

External Jars

Verifying That JUnit Works Appropriately In Eclipse

#1) In order to cross-check and confirm if the required library files were added in Eclipse, you may follow the below approach:

  • Right-click the project folder,
  • Select Build Path,
  • Then select Configure Build,
  • Select the tab ‘Libraries’, under the JRE Library, verify if all JUnit related jar files are in place.
  • If yes, then you are all set with the setup of JUnit in your Eclipse and you are now good to test a basic JUNIT program.

#2) Now add a simple JUnit test and see if it executes as expected. However, if executing your JUnit test file or Java test file results into NoClassDefFoundError, it implies that the dependent file(s) are missing from the classpath.

You might have to verify on the classpath accuracy starting from the Environment variables to the library files in Eclipse.

#3) Sometimes cleaning up the project folder also helps in getting rid of the NoClassDefFoundError. This can be done by selecting Project => Clean. Doing this firstly refreshes the current project folder and then, builds the workspace again.

JUnit 5 Architecture

Note: All the programs explained in the prior sections pertain to JUnit 4. From here on, we shall have a focussed understanding of JUnit 4 vs JUnit 5.

JUnit 5 works with Java 8 or above, hence, it supports lambda expression of Java code as well. It is the next generation of JUnit that comes with different versions named as following:

  • JUnit Platform
  • JUnit Jupiter
  • JUnit Vintage

JUnit 5 – Architecture

#1) JUnit Platform:

JUnit Platform for JUnit 5 works with the usage of two APIs – one that enables users to create a testing framework and another that helps users to launch a test through the IDE and through the command line.

#2) JUnit Jupiter:

This is a new version that comes with multiple and extensive lists of JUnit annotations.

#3) JUnit Vintage

Sometimes, all that we need is that the older version of JUnit code works as it worked earlier despite the version up-gradation. This basically is used when there are situations to migrate from JUnit 3 or 4 to JUnit 5.

Setup JUnit 5 In Eclipse

JUnit 4 installation and setup has been already covered towards the initial sections of this tutorial.

There are Maven dependencies for installing the JUnit 5 – Platform, Jupiter and Vintage versions on the URL https://junit.org/junit5/

You will find all the in and out details of including the Maven dependencies and setting up a Maven project in a separate topic covered towards the end of the JUnit series. JUnit 5 setup and installation will certainly be discussed in the topic.

JDK Version Requirement: JUnit 4 vs JUnit 5

In the below grid, we have jotted down the JDK version required per the JUnit version.

JUnit versionJDK version required
JUnit 4 Java 5 or above
JUnit 5Java 8 or above

Conclusion

This was all about a detailed understanding of how to download, install and set up JUnit. There was just a brief understanding of JUnit 5 however its setup details will be covered in the tutorial for Java Maven.

In the next tutorial, we will learn how to create a JUnit test case along with JUnit 4 test examples.

=> Visit Here For The Exclusive JUnit Training Tutorial Series.

Was this helpful?

Thanks for your feedback!

Leave a Comment