How To Download and Install JUnit?

JUnit is one of the most popular frameworks, widely-used by Java Developers. We may use the automated framework for both unit testing and UI testing. JUnit focuses on the idea of first testing and then coding further to increase the productivity and stability of the codes. Here in the article, we will learn how to download and install JUnit onto our local machine.

This article will cover and elaborate on the following topics:

  • What is JUnit?
  • Download and Install JUnit

What Is JUnit? 

JUnit is an open-source Unit Testing framework for Java. Here, as the term “Unit Testing” suggests, it is a way of testing a unit. Here, a unit refers to the smallest piece of code that can be fetched out of the system. It can be a line of the code, a method, or a class. The smaller the chunk of code, the better it is. If the tested units are small, the tests tend to run faster, providing a better insight into the code and its performance. 

Java developers use the testing framework to write and execute tests. We use it for unit testing of the source code, and JUnit works on testing a small part, then coding that and continuing the same till the end. 

Every time a new code is added, all the test cases have to be re-executed to ensure that nothing is broken. There are graphs in JUnit that show the progress of the test. The graphs show green when the test runs smoothly, and it turns red when the test fails. JUnit Testing helps developers to develop more reliable, readable, and bug-free code.

After knowing the basics of JUnit, let’s see how to download and install JUnit.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Download and Install JUnit

Prerequisites: 

Make sure that you have the following in your computer system:

  • JDK
  • Eclipse

Steps to download and install JUnit:

1. Open your browser:

  • In the browser window type “Download JUnit”

https://junit.org/junit4/

  • You will find “About JUnit” as the first option.
  • Go click on that link.
  • There you will find the “Download and Install” option.
  • In that link you will find two jar files:
  1. junit.jar
  2. Hamcrest.jar
  • Download the two jar files one after the other.

Download JUnit

2. Now open the location where those two jar files are downloaded.

  • Copy the two files.
  • Go to your “Documents”.
  • Then open the “Java Library” folder.
  • Paste the jar files here.

Java Library

3. Now open Eclipse:

  • In the Eclipse window create a new Java project.
  • Go to File > New > Java project.
  • In the dialog box that appears, give a project name.
  • Let’s name it “JUnit project”
  • Click on Next and Finish

Create Java Project

4. Then on the left side of your eclipse window:

  • Right-click on the Java project. 
  • In the drop-down menu, select Build Path.
  • In that menu select Configure Build Path.

Configure Build Path

  • On the window that appears on selection, navigate to the Libraries tab.
  • Then select the “Add External JARs” option.
  • Then in the box that appears select the two jar files and open them.

5. You will see the two jar files appear there, then click on apply and close the window.

Jar Files

6. Now, the project that appears with the name “JUnit Installation.

  • Right-click on that project and click on refresh.
  • Right-click on the “srs” folder and click on “new”.
  • In the “new” option click on the “package” option.
  • In the window that appears on selection, enter the name for a new package.
  • Let's name it “JUnit Install”

New Package

7. Now go inside that package and create a new class.

  • In the dialog box that appears, enter the Class Name.
      • Let's name the class “JUnit Class”.
      • Click on Finish.

8. Now inside the class, create a method.

public void myJUnitInstall()

{

System.out.println(“Welcome to my JUnit Testing”);

Method JUnitTesting

9. Now, add a library over this method 

@Test

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

10. You will find an error sign there. So, when you hover over this library you can see “Import ‘Test’ (org.unit)”. 

  • Click this option and you will find that the error disappears.

Library Test

Now it's time to run our dummy test.

11. Right-click on the method

  • In the dropdown menu select “Run As”.
  • And select “JUnit Test”.

Run as unit test

12. Then in the dialog box that appears click on OK and you can see the test being executed.

  • In the console, we can see “Welcome to my JUnit Testing”.

JUnit console

  • Then on the right side, you can see the JUnit Report showing the number of Runs, Errors, and Failures.

JUnit Report

You may add any number of cases and commands in the method part and test any particular part out of them using JUnit.

Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.

Conclusion

By now, you would know what is meant by the term JUnit Testing. Then, we also saw and understood the way to download and install JUnit using Eclipse. In the demo, we also explored how a small basic printing method could be tested with JUnit.

If you have any questions on the topic, do place them below in the comment section of this “How to Download and Install JUnit” article. We’ll get our SMEs to answer them for you as soon as possible!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.