, , ,

— Open source framework to write , organise and run test cases.

—  A Framework has scripts, excel sheets, logs , repositories etc. To use all of these together, we need a central controller which can be done using JUnit or TestNg.

— JUnit provides:

  • Annotation to identify the test methods.
  • Assertions for testing expected results.
  • Test runners for running tests.

— There is no main function in JUnit, it has annotations.

Some of the most commonly used annotations are:

@Test : This annotation when used in front of any method makes it a test case.

@Before: The method which has this annotation will be called before every method with @Test annotation.

@After: The method which has this annotation will be called after every method with @Test annotation.

@BeforeClass: the method which has this annotation will be the first function called in the class always. The method for this annotation  should be static in nature.

@AfterClass: the method which has this annotation will be the last function called in the class always.The method for this annotation  should be static in nature.

@Ignore: The method which has this annotation will be ignored while running.

@Before, @After, @BeforeClass, @AfterClass annotations are used to define order for test cases. @BeforeClass can be used for set-up activities and @AfterClass can be used for tear-down activities.

Working with JUnit:

  • Create a new Java Project in Eclipse.
  • Right Click on Project > Properties > Java Build Path >  Add Library and Select JUnit. JUnit comes inbuilt in eclipse.
  • When it is added, it will be shown on the Project.
  • Create a Class now without a main function.

Usage of annotations in JUnit can be seen in following code:

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class TestClass {

public static void beforeclasstest(){
System.out.println(" @BeforeClass");

public static void afterclasstest(){

public void beforetest(){
System.out.println("@Before ");

public void aftertest(){

public void testFunction(){
System.out.println(" First Test Annotation ");

public void testFunctiona(){

System.out.println("Second Test Annotation");


The output is

First Test Annotation

JUnit has an annotation to run the tests according to the ascending alphabetical order. It  can be used in following way:

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


public class TestClass1 {

public void firstTest(){
System.out.println("This is the first test");

public void secondTest(){
System.out.println("This is the second test");

public void first1(){
System.out.println("This is inside firstTest1 method ");

public void secondTesta(){
System.out.println("This is inside secondTesta method");

Output is :

This is inside firstTest1 method
This is the first test
This is the second test
This is inside secondTesta method

Using Assertions:

JUnit provides an assert class with various methods to compare objects , strings, doubles etc. for equality. If the objects are equal, the test is successful. Otherwise, AssertionError is thrown.

Very basic usage of this:

import static org.junit.Assert.*;

import org.junit.Test;
public class TestClass4 {
public void testAssert(){

String x = "Ruma";

assertEquals("Rum",x);  // This will throw an error and program execution stops here.


Creating a TestSuite:  

You can run multiple classes using JUnit runner class. If you wish to run three tests TestClass.class, TestClass1.class and TestClass3.class, it can be done like this:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;


public class MyRunner {