The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

JUNIT - ENVIORNMENT SETUP


Try it Online Option

We already have set up Java programming environment online, so that you can compile and execute all the available examples online at the same time while you are doing your theory work. It gives you confidence in what you are reading and verify the programs with different options. Feel free to modify any example and execute it online.

Try the following example using our online compiler option available at http://www.compileonline.com/.

public class MyFirstJavaProgram {
public static void main(String []args) {
System.out.println("Hello World");
}
}

For most of the examples given in this tutorial, you will find a Try it option in our website code sections at the top right corner that will take you to the online compiler. So just make use of it and enjoy your learning.

Local Environment Setup

JUnit is a framework for Java, so the very first requirement is to have JDK installed in your machine.

System Requirement
JDKMemoryDisk SpaceOperating System
1.5 or above.No minimum requirement.No minimum requirement.No minimum requirement.
Step 1: Verify Java Installation in Your Machine

First of all, open the console and execute a java command based on the operating system you are working on.
OSTaskCommand
WindowsOpen Command Consolec:\> java -version
LinuxOpen Command Terminal$ java -version
MacOpen Terminalmachine:~ joseph$ java -version
Let's verify the output for all the operating systems:
OSOutput
Windowsjava version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07) Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
Linuxjava version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07) Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
Macjava version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b07) Java HotSpot(TM)64-Bit Server VM (build 17.0-b17, mixed mode, sharing)
If you do not have Java installed on your system, then download the Java Software Development Kit (SDK) from the following link:

http://www.oracle.com/technetwork/java/javase/downloads/index.html
We are assuming Java 1.6.0_21 as the installed version for this tutorial.

Step 2: Set JAVA Environment

Set the JAVA_HOME environment variable to point to the base directory location where Java is installed on your machine. For example,
OSOutput
WindowsSet the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.6.0_21
Linuxexport JAVA_HOME=/usr/local/java-current
Macexport JAVA_HOME=/Library/Java/Home
Append Java compiler location to the System Path.
OSOutput
WindowsAppend the string C:\Program Files\Java\jdk1.6.0_21\bin at the end of the system variable, Path.
Linuxexport PATH=$PATH:$JAVA_HOME/bin/
Macnot required
Verify Java installation using the command java -version as explained above.

Step 3: Download Junit Archive

Download the latest version of JUnit jar file from http://www.junit.org. At the time of writing this tutorial, we have downloaded Junit-4.10.jar and copied it into C:\>JUnit folder.
OSArchive name
Windowsjunit4.10.jar
Linuxjunit4.10.jar
Macjunit4.10.jar
Step 4: Set JUnit Environment

Set the JUNIT_HOME environment variable to point to the base directory location where JUNIT jar is stored on your machine. Let’s assuming we've stored junit4.10.jar in the JUNIT folder.
OSDescription
WindowsSet the environment variable JUNIT_HOME to C:\JUNIT
Linuxexport JUNIT_HOME=/usr/local/JUNIT
Macexport JUNIT_HOME=/Library/JUNIT
Step 5: Set CLASSPATH Variable

Set the CLASSPATH environment variable to point to the JUNIT jar location.
OSDescription
WindowsSet the environment variable CLASSPATH to
%CLASSPATH%;%JUNIT_HOME%\junit4.10.jar;.;
Linuxexport
CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:.
Macexport
CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.10.jar:.
Step 6: Test JUnit Setup

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);
}
}

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());
}
}

Step 7: 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 as follows:

C:\JUNIT_WORKSPACE>java TestRunner

Verify the output.

true
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com