03 - JUnit installation

3 JUnit installation

          3.1 Environment Setup        

          3.2 Download JUnit library

          3.3 Run JUnit from command line

 

JUnit is already integrated in several IDE’s but it can be also used manually from command line.

JUnit is available as a JAR file. The jar file contains all the necessary classes and must be located so as the compiler could find it.

In case of using JUnit from command line, the CLASSPATH has to be set in order to include the JUnit jar.

3.1 Environment Setup

Since JUnit is a framework for Java, before starting to use it, JDK has to be installed on the machine.

The JDK version has to be minimum 1.5.

3.2 Download JUnit library

 JUnit can be used directly by downloading the latest version of JUnit from the JUnit website:

       http://junit.org/

For example, for a 4.x version the JUnit library is contained in junit-4.*.jar. This library has to be added to the developed Java project and to the classpath.

The tests are not contained in the junit.jar but in the installation directory directly. Make sure that the installation directory is on the classpath.

Don’t install the junit.jar into the extension directory of the JDK installation. If this is done the test class on the files system will not be found.

To install JUnit on Windows follow these steps:

  1. Unzip the juni.zip distribution file to a directory of your choice represented by the classpath variable

       %JUNIT_HOME%

  1. Add JUnit to the classpath:

        Set CLASSPATH=%CLASSPATH%;%JUNIT_HOME%\junit.jar

To install JUnit on Unix(bash), follow the same step as on Windows at 1 and :

Add JUnit to the classpath

       export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit.jar

Example:

       set CLASSPATH=C:\junit\junit4.12\junit-4.12.jar

3.3 Run JUnit from command line

Create a java class with the name JUnitTest in the folder C:\Junit

package main.java.util;

import static org.junit.Assert.*;

import org.junit.Test;

public class JunitTest {
    @Test
       public void isFunctioning() {
          String st= "Junit is functioning";
          assertEquals("Junit is functioning",st);
       }
}

Create other java class file with the name TestRunner in the same folder C:\Junit in order to execute the Test cases.

package test.java;
import main.java.util.JunitTest;

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(JunitTest.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      System.out.println(result.wasSuccessful());
   }
}

The class TestRunner.java can be compiled with javac compiler

       C:\junit>javac Junittest.java TestRunner.java

Run the class TestRunner.java from the directory where the JUnit was unzipped:

       C:\junit>java TestRunner

The result is:

       true

To check if JUnit is installed correctly a source file containing the following import statement has to be compiled:

       import junit.framework.*;

If the compiler succeeds then JUnit library can be found. 

Like us on Facebook