Today's Offer - QA Certification Training - Enroll at Flat 10% Off.

- QA Testing Blogs -

Junit Testing Tutorial For Beginners

The 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.

What is JUnit?

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

  • It is an open-source framework which is used for writing and running tests
  • It provides annotations to identify test methods
  • It provides assertions for testing the expected results
  • It provides test runners to run tests
  • It allows the tester to write code faster which increases the quality
  • These tests are run automatically, check their results, and provide immediate feedback
  • These show test progress in a green color bar if the test is running smoothly, and it turns red if the test fails
QA curriculum

How to Download and Install 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: Difference Between Agile, Scrum, Kanban, and Waterfall
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

Read: Integration Testing Tutorial Guide for Beginner

<dependency>
<groupId>JUnit</groupId>
<artifactId>JUnit</artifactId>
<version>4.12</version>
</dependency>

b). JUnit Gradle Dependency


dependencies {
testCompile 'JUnit:JUnit:4.12'
}

c). JUnit Jar File

Click the link to download the JUnit 4.12 jar file.

JUnit Test Framework

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.

QA Quiz

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 used
  • tearDown() method, runs after every test method. The annotation @After is used

The 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-

  • The code is not readable
  • The code is not easy to maintain
  • When the test suite will be complex, the code will contain logical issues

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 methods
  • TestCase: It contains a test case that defines the fixture to run multiple tests
  • TestResult: It contains methods to collect the results of executing a test case

JUnit Annotations & API

An 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:

Read: Top 20 Frequently Asked Regression Testing Interview Questions & Answers
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: -

  1. A method annotated with @BeforeClass
  2. A method annotated with @Before
  3. A first method annotated with @Test, i.e. test1().
  4. A method annotated with @After
  5. A method annotated with @Before
  6. A second method annotated with @Test, i.e. test2().
  7. A method annotated with @After
  8. A method annotated with @AfterClass

JUnit Parameterized Test

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: -

  1. Annotate test class with @RunWith(Parameterized.class).
  2. Create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set.
  3. Create a public constructor that takes in what is equivalent to one "row" of test data.
  4. Create an instance variable for each "column" of test data.
  5. Create your test case(s) using the instance variables as the source of the test data.

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

JUnit ErrorCollector

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 Exception Test

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:-

  1. Set the expected parameter @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();
    }
  1. Using try catch

@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)"));
        }                 
    }
  1. Testing with ExpectedException Rule.

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

JUnit Ignore Test

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.

Writing Tests in JUnit

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

JUnit Test Suites

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 {  
} 
QA Demo Class

JUnit Assertions

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: -

  • JUnit.Assert.assertArrayEquals
  • JUnit.Assert.assertEquals
  • JUnit.Assert.assertFalse
  • JUnit.Assert.assertNotNull
  • JUnit.Assert.assertNotSame
  • JUnit.Assert.assertNull
  • JUnit.Assert.assertSame
  • JUnit.Assert.assertTrue
the org.JUnit.Assert.assertThat method (available in JUnit4) which uses matchers and is better than old style assertions because it provides:
  • Better readability
    • assertThat(actual, is(equalTo(expected)));is better than assertEquals(expected, actual);
    • assertThat(actual, is(not(equalTo(expected))));is better than assertFalse(expected.equals(actual));
  • Better failiure messages
    • lang.AssertionError: Expected: is "hello" but: was "hello world"is better than

org.JUnit.ComparisonFailure: expected:<hello[]> but was:<hello[ world]>

  • Flexbility
    • Multiple conditions could be asserted using matchers like anyOfor allOf.

import static org.JUnit.Assert.*;
@Test
public void method() {
assertTrue(new ArrayList().isEmpty());
}

JUnit Assumptions

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

Summary

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: Unit Testing Interview Questions and Answers

    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

8 days 14 Dec 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

-1 day 05 Dec 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

-1 day 05 Dec 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

0 day 06 Dec 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

14 days 20 Dec 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

18 days 24 Dec 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

3 days 09 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

0 day 06 Dec 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

3 days 09 Dec 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on QA Testing Course

Interviews