JUnit is a unit testing library/framework for Java. Why do we need a unit testing framework. Unit testing refers to testing the functionality of a class or method at the developer level. The developer is writing a class and needs to make sure that the
class is working as designed. The developer can write a manual test case and test the class. However the developer will have to manually run the test class. What if there are 100 classes to be tested. The JUnit framework allows us to organize the tests and provides annotations and methods to make the unit process more streamlined.
Setting up the environment
We will be working with JUnit4 and JUnit5. We need JDK 1.8 or higher. You can check if you have the JDK already installed by opening up command prompt in Windows or the Terminal application in Mac or the Unix console / terminal window and type in
javac -version
You should get a result such as:
C:\Java>javac -version
javac 1.8.0_121
If you get an error such as "command not found" then either it is not installed or it is installed and you don't have the PATH environment variable set. You can check some folders such as:
C:\Program Files
C:\Program Files (x86)
and check for a JDK installation. You might find it in a path such as:
C:\Program Files\Java\jdk1.8.0_211
If you don't have it then download the JDK from the Oracle site at:
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
You must download the one for your operating system. It can be installed anywhere. We are going to assume that you have installed it on the folder "C:\jdk8" .
We are going to use a particular structure for the JUnit libraries and projects. You don't have to use this structure and if you have prior experience with Java projects then you are welcome to use a different structure.
Installation of JUnit 4
We are going to discuss the steps for the Windows system. You can use the same steps for Mac or Unix but the file paths will be different.
Now create a folder named "JUnit" to work with your JUnit related files. In the JUnit folder create a folder called "lib" .
Copy the "junit-4.12.jar" and "hamcrest-core-1.3.jar" to this folder.
Download the JUnit jar from :
https://sites.google.com/site/ajaymittalinstructor/junit/junit-4.12.jar?attredirects=0&d=1
and the "hamcrest-core-1.3.jar" from the below link.
https://sites.google.com/site/ajaymittalinstructor/junit/hamcrest-core-1.3.jar?attredirects=0&d=1
You should have the following structure.
C:\JUnit
\lib junit-4.12.jar
hamcrest-core-1.3.jar
Open up the Windows prompt and "cd" to the "JUnit" folder and type in
javac -version
If you installed JDK 8 and are still seeing this error then create a file called "set1.bat" in the JUnit folder
and write the following into that file.
set path=c:\jdk_18\bin;%path%
Save the file.
Open a command console. and run it by typing "set1.bat" in the command prompt.
c:\JUnit> set1.bat
Then try to run the command "javac -version" again.
What we are doing with "set1.bat" is setting the path for that particular Dos session. If you exit the Dos session and start a new one then you will have to run the file "set1.bat" again. This is useful if we have several different JDK versions and we want to select the one to use. We can also set the path permanently by modifying it from the environment variables screen in Windows. In the Windows search bar type in the word" Environment" and you will see a menu option come up that says
"Edit Environment variables for this account.". Select this option and a screen will come up. You can choose to modify the "PATH" variable for your user or the System "PATH" variable. Now when we open the Dos session the JDK path will already be set.
Let us write our first program to test out JUnit4 ! Create a new folder in JUnit called "test1" and we are going to place our
Java files for our first program. We will create a separate folder called "test2" , "test3" an so on for rest of our programs.
Our program is going to consist of 3 files( downloadable from the below link).
Calculator.java
CalculatorTest.java
TestRunner.java
The "Calculator.java" contains our class that we want to test and the "CalculatorTest.java" contains our test cases and finally we run the test cases using the "TestRunner.java" .
We can compile and run the program using the following 3 lines:
set path=c:\jdk18\bin;%path%
javac -cp c:\junit\lib\* *.java
java -cp "c:\junit\lib\*;." TestRunner
If you want you can copy the above lines in a file "run1.bat" in the same folder and run the file . A sample output might be:
C:\junit\test1>run1.bat
C:\junit\test1>set path=c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;c:\jdk18\bin;C:\apache-maven-3.6.3\bin;c:\jdk18\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\dotnet\;C:\Users\user\AppData\Local\Microsoft\WindowsApps;
C:\junit\test1>javac -Xlint:deprecation -cp c:\junit\lib\* *.java
C:\junit\test1>java -cp "c:\junit\lib\*;." TestRunner
true
We do not need the first line if you already ran the command in the session window. Let us briefly look at the classes