• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

Installing Junit is a 6 part process. It is explained in detailed below-

PART 1) Install Java

JUnit is a Testing framework used to test Java based application. So before installing JUnit, you need to configure or verify java development kit (JDK) in your machine.

Click this tutorial to download and install Java

PART 2) Download JUnit

Step 1) Visit http://junit.org/junit4/ and click Download and Install

How to Download and Installation JUnit

Step 2) Click junit.jar

How to Download and Installation JUnit

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 jar link to download Junit version 4.12 as shown below

How to Download and Installation JUnit

Step 4) Visit https://github.com/junit-team/junit4/wiki/Download-and-Install again. Click hamcrest-core.jar

How to Download and Installation JUnit

Step 5) Download the Jar

How to Download and Installation JUnit

For JUnit installation, you need JUnit jars, and you can download the desired version of JUnit jar file from JUnit official youbsite http://www.junit.org

Here is the jars list:

  • JUnit.jar
  • hamcrest-core.jar

PART 3) JUnit Environment Setup

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

For example, if you have created a JUnit folder in c: drive and placed jars there, then for environment settings you need to open control panel ->advanced ->environment variable.

  1. Under environment window clicks on "new" button.

How to Download and Installation JUnit

When you click on new button in environment variables, it will open another window

Step 2) A "New System Variable" window will open:

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

How to Download and Installation JUnit

When you click on OK, it will create a new system variable with the given name and value. Which you can verify in environment variable window as shown in step 1 image.

Step 3) After creating JUNIT_HOME, create another variable with the name CLASSPATH. Again go to Environment Variables and follow the below steps.

  1. Click on "new" button. When you click on new in environment variables, it will open another window.

    How to Download and Installation JUnit

Step 4) In this step, point out JUNIT_HOME to JUnit.jar which is placed in JUnit folder as given below:

  1. Variable Name: JUNIT_HOME
  2. Variable Value: %CLASSPATH%;%JUNIT_HOME%\JUnit4.10.jar;.;
  3. Click on the OK button.

How to Download and Installation JUnit

Step 5) Once you click on the 'OK' button, you can verify that a new environment variable named "CLASSPATH" can be seen under system variable. See below

How to Download and Installation JUnit

PART 4) Install JUnit jar file in eclipse

Step 1) Right click on project:

  1. Click on "build path" and then
  2. Click on "Configure build path".

How to Download and Installation JUnit

Step 2) In this step,

  1. Go to java build path window as shown in below figure
  2. Now click on "Add External JARs" button to add yyour downloaded JUnit.jar file with eclipse.

After adding a JUnit.jar file, click on 'OK' button to close java build path window.

How to Download and Installation JUnit

Part 5) Verifying whether required jar file for JUnit is in my build path

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

  1. Right click on project -> Build Path
  2. Click on "Configure build path".

How to Download and Installation JUnit

Step 2) Java build path window will appear as shown below.

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

Once you expand JUnit libraries, you can see java libraries as shown below:

How to Download and Installation JUnit

Now you are ready to use JUnit with eclipse.

PART 6) Verify JUnit setup

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

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

How to Download and Installation JUnit

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 above test.

How to Download and Installation JUnit

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 below steps:

  1. Right click on TestRunner.java and click on "Run As" as shown below
  2. Another window will be open once you click on "Run As", click on "1 JUnit Test" as shown below:

How to Download and Installation JUnit

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

How to Download and Installation JUnit

Click to download the above code for your testing purposes.

 

YOU MIGHT LIKE: