May 15, 2021 jUnit
1. Junit - Environment settings
4. Step 1: Verify the Java appliance in your machine
5. Step 2: Set up the JAVA environment
6. Step 3: Download the Junit archive
7. Step 4: Set up the JUnit environment
JUnit is a framework for Java, so the most fundamental need is to have JDK installed in your machine.
Jdk | 1.5 or more |
---|---|
Memory | There are no minimum requirements |
Disk space | There are no minimum requirements |
Operating system | There are no minimum requirements |
Now open the console and perform the following java requirements.
Operating system | Task | Command |
---|---|---|
Windows | Open the command operator's station | c:>java -version |
Linux | Open the command terminal | $ java -version |
Mac | Open the terminal | machine:~ joseph$ java -version |
Let's verify the output of all operating systems:
Operating system | Output |
---|---|
Windows |
java version "1.6.0-21"
Java (TM) SE Operating Environment (build 1.6.0-21-b07) Java Hotspot (TM) Client Virtual Machine (build 17.0-b17, hybrid mode, shared) |
Linux |
java version "1.6.0-21"
Java (TM) SE Operating Environment (build 1.6.0-21-b07) Java Hotspot (TM) Client Virtual Machine (build 17.0-b17, hybrid mode, shared) |
Mac |
java version "1.6.0-21"
Java (TM) SE Operating Environment (build 1.6.0-21-b07) Java Hotspot (TM) 64-Byte Server Virtual Machine (build 17.0-b17, hybrid mode, shared) |
If you haven't installed Java yet, install the Java SDK http://www.oracle.com/technetwork/java/javase/downloads/index.html at the following URL. We use Java 1.6.0-21 as the installed version of this tutorial.
Set JAVA_HOME environment variable to point to the base directory location, where Java is installed on your machine.
OS | Output |
---|---|
Windows |
Set the environment variable JAVA_HOME to C:\Program
Files\Java\jdk1.6.0_21 |
Linux | Output JAVA_HOME //usr/local/java-current |
Mac | Output JAVA_HOME s/Library/Java/Home |
The system path adds the Java compiler location.
OS | Output |
---|---|
Windows |
Add a string at the end of the system variable path
; C:\Program Files\Java\jdk1.6.0_21\bin |
Linux | Output PATH- $PATH: $JAVA/bin/ |
Mac | No, you don't need to |
Use the Java-version command explained above to verify the Java installation.
Download the http://www.junit.org version of JUnit's compressed file from the computer. While writing this tutorial, I've downloaded Junit-4.10 .jar and copied it to the C:;JUnit folder.
OS | The name of the file |
---|---|
Windows | junit4.10.jar |
Linux | junit4.10.jar |
Mac | junit4.10.jar |
Set JAVA_HOME environment variable to point to the base directory location, where junIT compressed files are installed on your machine. Suppose we have stored junit4.10 files in the JUNIT folders of the following .jar.
OS | Output |
---|---|
Windows | Set the environment variable JUNIT_HOME to C: .JUNIT |
Linux | Output JUNIT_HOME //usr/local/JUNIT |
Mac | Output JUNIT_HOME s/Library/JUNIT |
Set the CLASSPATH environment variable to point to the JUNIT compressed file location. Suppose we have stored junit4.10 in the JUNIT folders of the following .jar.
OS | Output |
---|---|
Windows | Set the environment variable CLASSPATH to %CLASSPATH; %JUNIT_HOME%\junit4.10.jar;.; |
Linux | Output CLASSPATH: $CLASSPATH: $JUNIT.HOME/junit4.10.jar:. |
Mac | Output CLASSPATH: $CLASSPATH: $JUNIT.HOME/junit4.10.jar:. |
In C: Create a JUNIT_WORKSPACE file named TestJunit in this file.
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);
}
}
In C: The JUNIT_WORKSPACE java class file, named TestRunner, to execute the test case.
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());
}
}
Use the javac compiler to write classes as follows.
C:\JUNIT_WORKSPACE>javac TestJunit.java TestRunner.java
Now run Test Runner to see the results.
C:\JUNIT_WORKSPACE>java TestRunner
Verify the output.
ture