Whatever code we write, it needs to be tested properly before going to production. We can do it on a staging level or the developer can write test cases to remove the errors in development level. Each technology have testing frameworks that allows developers to write clean and tested code.

About JUnit

JUnit is a unit testing framework for the Java Programming Language. It is an open source framework that allow us to write simple test cases. It provides annotations and assert methods to quickly and easily test our code.

To test a code, we write test cases. Each test case specifies an input and gets an expected output. If the output is not according to expectation, the test case fails.

Here are some annotations we are going to use while creating a test:

  1. @BeforeClass
    The method with this annotation is executed once before any of the test methods in the class.
  2. @AfterClass
    The method with this annotation is executed once after all the test methods are executed in the class.
  3. @Before
    The method with this annotation is executed just before the method annotated with @Test.
  4. @After
    The method with this annotation is executed just after the method annotated with @Test.
  5. @Test
    The method that has the actual test case.

Let us check the life cycle of JUnit testing.

In Eclipse, File -> New -> Project -> Maven Project

Select maven-archetype-quickstart as the Archetype.

Group Id     :   test
Artifact Id  :   unit
Package      :   test.unit

You will see two folders:

src/main/java  :    This will contain our code classes

src/main/test   :    This will contain our test cases


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">







Create a new class named LifeCycleTest in the package test.unit under src/test/java folder.

Add following code to it:

package test.unit;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class LifeCycleTest 
     public static void beforeClass(){
          System.out.println("Before Class");
     public static void afterClass(){
          System.out.println("After Class");
     public void before(){
     public void after(){
     public void firstTest(){
          System.out.println("First test");
     public void secondTest(){
          System.out.println("Second test");

Now right click anywhere in your code, Run As -> JUnit Test

You should see the output telling that both the test cases passed. Also, in the console, you will see the life cycle.

Now, let’s test an actual class.

Create a new class named Basic in the package test.unit under src/main/java. Here is the code:

package test.unit;

public class Basic {

    public static int big(int a, int b){
       return a>b ? a:b;
    public static int add(int a,int b){
       return a+b;
    public static int factorial(int n){
       int f = 1;
       for(int i=1;i<=n;i++)
           f = f*i;
       return f;

Now, create a new test class named BasicTest in the package test.unit under src/test/java. Add following code to it:

package test.unit;

import org.junit.Test;

import static org.junit.Assert.*;

public class BasicTest {

    public void testBig(){
       assertEquals(5, Basic.big(5, 3));

    public void testAdd(){
       assertEquals(8, Basic.add(5, 3));

    public void testFactorial(){
       assertEquals(120, Basic.factorial(5));

Run the test cases.

assertEquals method compares the values passed to it. First is the expected result and second is the result to be compared.