Mockito – Environment Setup

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

System Requirement

JDK1.5 or above.
Memoryno minimum requirement.
Disk Spaceno minimum requirement.
Operating Systemno minimum requirement.

Step 1 − Verify Java Installation on Your Machine

Open the console and execute the following java command.

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, download and install the Java Software Development Kit (SDK).

We assume you have Java 1.6.0_21 installed on your system 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 the location of the Java compiler to your System Path.

OSOutput
WindowsAppend the string; C:\Program Files\Java\jdk1.6.0_21\bin to 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 Mockito-All Archive

To download the latest version of Mockito from Maven Repository.

Save the jar file on your C drive, let’s say, C:\>Mockito.

OSArchive name
Windowsmockito-all-2.0.2-beta.jar
Linuxmockito-all-2.0.2-beta.jar
Macmockito-all-2.0.2-beta.jar

Step 4 − Set Mockito Environment

Set the Mockito_HOME environment variable to point to the base directory location where Mockito and dependency jars are stored on your machine. The following table shows how to set the environment variable on different operating systems, assuming we’ve extracted mockito-all-2.0.2-beta.jar onto C:\>Mockito folder.

OSOutput
WindowsSet the environment variable Mockito_HOME to C:\Mockito
Linuxexport Mockito_HOME=/usr/local/Mockito
Macexport Mockito_HOME=/Library/Mockito

Step 5 − Set CLASSPATH Variable

Set the CLASSPATH environment variable to point to the location where Mockito jar is stored. The following table shows how to set the CLASSPATH variable on different operating systems.

OSOutput
WindowsSet the environment variable CLASSPATH to %CLASSPATH%;%Mockito_HOME%\mockito-all-2.0.2-beta.jar;.;
Linuxexport CLASSPATH=$CLASSPATH:$Mockito_HOME/mockito-all-2.0.2-beta.jar:.
Macexport CLASSPATH=$CLASSPATH:$Mockito_HOME/mockito-all-2.0.2-beta.jar:.

Step 6 − Download JUnit Archive

Download the latest version of JUnit jar file from Github. Save the folder at the location C:\>Junit.

OSArchive name
Windowsjunit4.11.jar, hamcrest-core-1.2.1.jar
Linuxjunit4.11.jar, hamcrest-core-1.2.1.jar
Macjunit4.11.jar, hamcrest-core-1.2.1.jar

Step 7 − Set JUnit Environment

Set the JUNIT_HOME environment variable to point to the base directory location where JUnit jars are stored on your machine. The following table shows how to set this environment variable on different operating systems, assuming we’ve stored junit4.11.jar and hamcrest-core-1.2.1.jar at C:\>Junit.

OSOutput
WindowsSet the environment variable JUNIT_HOME to C:\JUNIT
Linuxexport JUNIT_HOME=/usr/local/JUNIT
Macexport JUNIT_HOME=/Library/JUNIT

Step 8 − Set CLASSPATH Variable

Set the CLASSPATH environment variable to point to the JUNIT jar location. The following table shows how it is done on different operating systems.

OSOutput
WindowsSet the environment variable CLASSPATH to %CLASSPATH%;%JUNIT_HOME%\junit4.11.jar;%JUNIT_HOME%\hamcrest-core-1.2.1.jar;.;
Linuxexport CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.11.jar:$JUNIT_HOME/hamcrest-core-1.2.1.jar:.
Macexport CLASSPATH=$CLASSPATH:$JUNIT_HOME/junit4.11.jar:$JUNIT_HOME/hamcrest-core-1.2.1.jar:.

Leave a Reply