16
AprThe tutorial will help the software testing aspirants with the usage of JUnit in unit testing while working with Java. The tutorial is designed for beginners to help them understand the basic functionality of JUnit.
Table of Content
It is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development and is one of a family of unit testing frameworks. The main usage of JUnit is to write repeatable tests for your application code units. JUnit stimulate the idea of “testing first, then coding” which accentuate on setting up of the test data for the code snippets that can be tested first and then implemented. The tet approach explicates –
JUnit increases the productivity of a programmer and the stability of the program’s code snippets, which reduces the time of the tester, which is spent on debugging of the code.
Features of JUnit
Local Environment Setup
JUnit is a framework for Java, so the very first requirement is to have JDK installed on your machine.
System Requirements
JDK | 1.5 or above. |
Memory | No minimum requirement. |
Disk Space | No minimum requirement. |
Operating System | 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.
OS | Task | Command |
Windows | Open Command Console | c:\> java -version |
Linux | Open Command Terminal | $ java -version |
Mac | Open Terminal | machine:~ joseph$ java -version |
Let's verify the output for all the operating systems −
OS | Output |
Windows |
java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101) |
Linux |
java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101) |
Mac |
java version "1.8.0_101" Java(TM) SE Runtime Environment (build 1.8.0_101) |
If you do not have Java installed on your system, then download the Java Software Development Kit (SDK) from the following link https://www.oracle.com. We are assuming Java 1.8.0_101 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-
Read: Software QA Tester Resume Sample for Experienced & Fresher
OS | Output |
Windows | Set the environment variable JAVA_HOME to C:\Program Files\Java\jdk1.8.0_101 |
Linux | export JAVA_HOME = /usr/local/java-current |
Mac | export JAVA_HOME = /Library/Java/Home |
Append Java compiler location to the System Path.
OS | Output |
Windows | Append the string C:\Program Files\Java\jdk1.8.0_101\binat the end of the system variable, Path. |
Linux | export PATH = $PATH:$JAVA_HOME/bin/ |
Mac | not 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.12.jar and copied it into C:\>JUnit folder.
OS | Archive-name |
Windows | JUnit4.12.jar |
Linux | JUnit4.12.jar |
Mac | JUnit4.12.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 assume we've stored JUnit4.12.jar in the JUNIT folder.
Sr.No | OS & Description |
1 | Windows: Set the environment variable JUNIT_HOME to C:\JUNIT |
2 | Linux: export JUNIT_HOME = /usr/local/JUNIT |
3 | Mac: export JUNIT_HOME = /Library/JUNIT |
Step 5: Set CLASSPATH Variable
Set the CLASSPATH environment variable to point to the JUNIT jar location.
Sr.No | OS & Description |
1 | Windows: Set the environment variable CLASSPATH to %CLASSPATH%;%JUNIT_HOME%\JUnit4.12.jar;.; |
2 | Linux: export CLASSPATH = $CLASSPATH:$JUNIT_HOME/JUnit4.12.jar:. |
3 | Mac: export CLASSPATH = $CLASSPATH:$JUNIT_HOME/JUnit4.12.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
To include JUnit into your project, you need to include its dependency into the classpath.
a). JUnit Maven Dependency
<dependency>
<groupId>JUnit</groupId>
<artifactId>JUnit</artifactId>
<version>4.12</version>
</dependency>
b). JUnit Gradle Dependency
Read: Penetration Testing Tutorial Guide for Beginners
dependencies {
testCompile 'JUnit:JUnit:4.12'
}
c). JUnit Jar File
Click the link to download the JUnit 4.12 jar file.
JUnit is a Regression testing framework which is used by developers to implement unit testing in Java to escalate the speed of programming and elevate the quality of code. The JUnit framework also allows quick and easy generation of test data and test cases.
The JUnit test framework provides the following important features:-
1). Test Fixtures
A fixture is a fixed state of a set of objects which is used as a baseline for running the test cases. The primary purpose of a test fixture is to fortify that there is a well-known and fixed environment in which tests are run so that results are in a repeated manner.
setUp()
method, runs before every test invocation. The annotation @Before
is usedtearDown()
method, runs after every test method. The annotation @After
is usedThe below code will execute two test cases on a simple file.
public class TestCaseFile
{
private File output;
output = new File(...);
output.delete();
public void testFile1()
{
//Code to verify Test Case 1
}
output.delete();
output = new File(...);
public void testFile2()
{
//Code to verify Test Case 2
}
output.delete();
}
There are few issues in the above code-
Let’s compare the same code using JUnit-
public class TestCaseFile
{
private File output;
@Before public void createOutputFile()
{
output = new File(...);
}
@After public void deleteOutputFile()
{
output.delete();
}
@Test public void testFile1()
{
// code for test case objective
}
@Test public void testFile2()
{
// code for test case objective
}
}
The code is now readable and maintainable. The above code structure is Text fixture.
2). Test Suites
To execute multiple test cases in a specific order, it is done by combining all the test cases in a single origin. The origin is known as test suites.
To run the test suite, you need to annotate a class using the annotations mentioned below-
@Runwith(Suite.class)
@SuiteClassses(test1.class,test2.class……) or @SuiteClasses({test1.class,test2.class})
Below example will run the TestJUnit1.class
and TestJUnit2.class
import org.JUnit.runner.RunWith;
import org.JUnit.runners.Suite;
//JUnit Suite Test
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestJUnit1.class ,TestJUnit2.class
})
public class JUnitTestSuite {
}
3). Test Runners
The Test Runner is used to execute the test cases. Let’s take an assumption of the test cases class TestJUnit.
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());
}
}
4). JUnit Classes
There are important classes which are used in writing and testing JUnits. Some of them are-
Assert:
It contains a set of assert methodsTestCase:
It contains a test case that defines the fixture to run multiple testsTestResult:
It contains methods to collect the results of executing a test caseAn annotation is a special form of meta-data in a syntactical manner that can be added to Java source code for the better readability and structure. Variables, parameters, packages, methods, and classes can be annotated. JUnit offers the following annotations to write tests:
ANNOTATION | DESCRIPTION |
@Before |
The annotated method will be run before each test method in the test class. |
@After |
The annotated method will be run after each test method in the test class. |
@BeforeClass |
The annotated method will be run before all test methods in the test class. This method must be static. |
@AfterClass |
The annotated method will be run after all test methods in the test class. This method must be static. |
@Test |
It is used to mark a method as a JUnit test |
@Ignore |
It is used to disable or ignore a test class or method from a test suite. |
@FixMethodOrder |
This class allows the user to choose the order of execution of the methods within a test class. |
@Rule |
Annotates fields that reference rules or methods that return a rule. |
@ClassRule |
Annotates static fields that reference rules or methods that return them. |
Following is the execution order of the above-mentioned methods when a JUnit4 test is run: -
Read: Various Career Oriented Testing Certification
@BeforeClass
@Before
@Test, i.e. test1().
@After
@Before
@Test, i.e. test2().
@After
@AfterClass
JUnit Parameterized test was the new feature which was introduced in JUnit 4. This allows the developer to run the same test, again using different values. A total of five steps are followed to create a parameterized test: -
Let’s look into an example.
import java.util.Arrays;
import java.util.Collection;
import org.JUnit.Test;
import org.JUnit.Before;
import org.JUnit.runners.Parameterized;
import org.JUnit.runners.Parameterized.Parameters;
import org.JUnit.runner.RunWith;
import static org.JUnit.Assert.assertEquals;
@RunWith(Parameterized.class)
public class PrimeNumber
{
private Integer i;
private Boolean expectedResult;
private PrimeNumberChecker primeNumberChecker;
@Before public void initialize()
{
primeNumberChecker = new PrimeNumberChecker();
}
// Each parameter should be placed as an argument here
// Every time runner triggers, it will pass the arguments
// from parameters we defined in primeNumbers() method
public PrimeNumberCheckerTest(Integer i, Boolean expectedResult)
{
this.i= i;
this.expectedResult = expectedResult;
}
@Parameterized.Parameters
public static Collection primeNumbers()
{
return Arrays.asList(new Object[][]
{
{ 2, true },
{ 6, false },
{ 19, true },
{ 22, false },
{ 23, true }
}
);
}
// This test will run 4 times since we have 5 parameters defined
@Test public void PrimeNumberCheckerTest()
{
System.out.println("Parameterized Number is : " + i);
assertEquals(expectedResult, primeNumberChecker.validate(i));
}
}
Create another class TestRunner to compile the program.
import org.JUnit.runner.JUnitCore;
import org.JUnit.runner.Result;
import org.JUnit.runner.notification.Failure;
public class Runner
{
public static void main(String[] args)
{
Result result = JUnitCore.runClasses(PrimeNumberCheckerTest.class);
for (Failure failure : result.getFailures())
{
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
Output
Parameterized Number is: 2
Parameterized Number is : 6
Parameterized Number is : 19
Parameterized Number is : 22
Parameterized Number is : 23
true
Normally, when you identify an error during execution of the test, you would stop the test, fix the error, and re-run the test. But in the case of JUnit, you can continue with the test execution even when you find an issue or test fails. ErrorCollector collects all error objects and reports it only once after the test execution is over.
While writing a test script, if you want to execute all the tests even if any line of code fails due to any network failure, or any other reason, in that situation, you can continue executing the test script by using a special feature provided by JUnit which is called ‘error collector.’
JUnit uses @Rule annotation, which is used to create an object of error collector. Once the object is created, you can easily add all the errors into the object using method addError (Throwable error). Throwable is a superclass of Exception and Error class in Java. When errors are added in this way, these errors will be logged in a JUnit test result.
The major benefit of adding all the errors in an ErrorCollector is that you can verify all the errors at once. And, if the script fails in the middle, it can continue executing it. But yes, it is important to note here that in case of try/catch block, using ErrorCollector won’t be possible.
JUnit provides the exception handling of code. You can test whether the code throws a desired exception or not. There are three different ways to test that a method would throw the expected exception:-
@Test(expected = FileNotFoundException.class).
would throw the expected exception:-
1. Set the expected parameter @Test(expected = FileNotFoundException.class).
@Test(expected = FileNotFoundException.class)
public void testReadFile() throws IOException {
FileReader reader = new FileReader("test.txt");
reader.read();
reader.close();
}
@Test
public void testReadFile2() {
try {
FileReader reader = new FileReader("test.txt");
reader.read();
reader.close();
fail("Expected an IOException to be thrown");
} catch (IOException e) {
assertThat(e.getMessage(), is("test.txt (No such file or directory)"));
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void testReadFile3() throws IOException {
thrown.expect(IOException.class);
thrown.expectMessage(startsWith("test.txt (No such file or directory)"));
FileReader reader = new FileReader("test.txt");
reader.read();
reader.close();
}
In case of the failure of the test case, the @Ignore
annotation helps. A test method annotated with @Ignore will not be executed. Also, if a test class is annotated with @Ignore
, then none of its test method will be executed.
Let’s see JUnit Ignore test into an action-
package com.mkyong;
import org.JUnit.Ignore;
import org.JUnit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.JUnit.Assert.assertThat;
public class IgnoreTest {
@Test
public void testMath1() {
assertThat(1 + 1, is(2));
}
@Ignore
@Test
public void testMath2() {
assertThat(1 + 2, is(5));
}
@Ignore("some one please create a test for Math3!")
@Test
public void testMath3() {
//...
}
}
In the above code snippet, both testMath()2
and testMath3()
will be ignored.
In JUnit, test methods are marked with @Test annotation. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. JUnit will report any exceptions thrown by the test as a failure. If no exceptions are thrown, the test is assumed to have succeeded.
import java.util.ArrayList;
import org.JUnit.After;
import org.JUnit.AfterClass;
import org.JUnit.Before;
import org.JUnit.BeforeClass;
import org.JUnit.Test;
public class Example {
@BeforeClass
public static void setup() {
}
@Before
public void setupThis() {
}
@Test
public void method() {
org.JUnit.Assert.assertTrue(new ArrayList().isEmpty());
}
@After
public void tearThis() {
}
@AfterClass
public static void tear() {
}
}
Using JUnit test suites, you can run tests spread into multiple test classes. In JUnit, both @RunWith
and @Suite
annotations are used to run the suite tests.
import org.JUnit.runner.RunWith;
import org.JUnit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestJUnit1.class,
TestJUnit2.class
})
public class JUnitTestSuite {
}
Assertions help in validating the expected output with the actual output of a test case. All the assertions are in the org.JUnit.Assert class. All assert methods are static, it enables better readable code. JUnit 4 has a set of following assertions: -
org.JUnit.ComparisonFailure: expected:<hello[]> but was:<hello[ world]>
import static org.JUnit.Assert.*;
@Test
public void method() {
assertTrue(new ArrayList().isEmpty());
}
Assumptions indicate the conditions in which a test is meaningful. A failed assumption does not mean the code is broken, but that the test provides no useful information. Assumptions basically mean “don’t run this test if these conditions don’t apply.” The default JUnit runner skips tests with failing assumptions.
import org.JUnit.Test;
import static org.JUnit.Assume.*;
public class Example {
public class AppTest {
@Test
void testOnDev()
{
System.setProperty("ENV", "DEV");
assumeTrue("DEV".equals(System.getProperty("ENV")));
}
@Test
void testOnProd()
{
System.setProperty("ENV", "PROD");
assumeFalse("DEV".equals(System.getProperty("ENV")));
}
}
}
JUnit is a unit testing framework for Java programming language. With this tutorial, you might have understood the basics of JUnit and scaled your unit testing practice like a pro. If not done yet, talk to our counselor to know more about QA Testing. Happy Testing!
Read: What Is The Difference Between Smoke And Sanity Testing?
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
AWS
DevOps
Data Science
Hadoop
Salesforce
QA
Business Analyst
MS SQL Server
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Trending Posts
Related Posts
How to Become a QA Software Tester?
462.5k
What is Unit Testing? Unit Testing Tutorial Guide for Beginners
380.3k
2020 Top 20 QTP Interview Questions and Answers
125.7k
Usability Testing Interview Questions & Answers for Fresher, Experienced
720.2k
Database Testing Interview Questions and Answers for Fresher and Experienced
303.4k
Receive Latest Materials and Offers on QA Testing Course
Interviews