Thursday, 21 April 2016

How to setup JUnit ?


1. Ensure Java is installed and JAVA_HOME environment variable is set to point to the base directory location where Java is installed.

2. Download latest version of JUnit jar file from http://www.junit.org.

3. Set the JUNIT_HOME environment variable to point to the base directory location where JUNIT jar is stored.
  Windows : Set the environment variable JUNIT_HOME to C:\JUNIT
  Linux : export JUNIT_HOME= /usr/local/JUNIT
  Mac : export JUNIT_HOME= /Library/JUNIT

4. Set the CLASSPATH environment variable to point to the JUNIT jar location.
  Windows : Set the environment variable CLASSPATH to %CLASSPATH%;%JUNIT_HOME%\junit4.10.jar;.;
  Linux : export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:.
  Mac : export CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:.

Setup is done. Next step to test the setup.



Test JUnit setup

1. Create a java class file name TestJunit in C:\ > JUNIT_WORKSPACE
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TestJunit {
   @Test
   public void testAdd() {
      String str= "Junit is working fine";
      assertEquals("Junit is working fine", str);
   }
}


2. Create a java class file name TestRunner in C:\ > JUNIT_WORKSPACE to execute Test case(s)
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.wasSuccessful());
   }
}      

3. Verify the Result
Compile the classes using javac compiler as follows
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java

Now run the Test Runner to see the result
C:\JUNIT_WORKSPACE>java TestRunner

Verify the output
true

No comments:

Post a Comment

Note: only a member of this blog may post a comment.