Sample test case program using JUnit

Hi Everyone, in this post, I will be writing a sample case using JUnit. JUnit is an unit testing framework for java application. JUnit has been important in developement of test driven developement, and is one of a family of unit testing framework which is collectively known as xUnit that is originated from sUnit.

Here, I am writing a sample test case program for the program conversion of String to Int in Java. Before starting the program we have to download the jar file of JUnit and put it in the lib folder. The program is as follows:

package com.program;
import com.program.*;
import static org.junit.Assert.*;

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

import com.program.InvalidNumberException;
import com.program.StringToInt;

public class StringToIntTestCase {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("SetUp BeforeClass");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("SetUp");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("tearDown");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("tearDown AfterClass");
    }

    @Test
    public void test_PositiveInteger() throws InvalidNumberException
    {
        assertEquals("Positive Integer", 123, StringToInt.StrToInt("123"));
    }
    
    @Test
    public void test_NegativeInteger() throws InvalidNumberException
    {
        assertEquals("Negative Integer", -123, StringToInt.StrToInt("-123"));
    }
    
    @Test
    public void test_Zero() throws InvalidNumberException
    {
        assertEquals("Zero", 0, StringToInt.StrToInt("0"));
    }

    @Test(expected = InvalidNumberException.class)
    public void test_Exception () throws InvalidNumberException
    {
        assertEquals("Exception",InvalidNumberException.class,StringToInt.StrToInt("hello"));
        
    }
}

The above test case contains 4 different test cases. Before explaining about test case, I am explaining about some annotations which are used in the test case.

5 different types of annotations are used :

  • @BeforeClass
@BeforeClass is used when there are lot of test cases in the program. It sets up a precondition that needs to be executed before executing the Test Case class. @BeforeClass is executed only once in the program.
  • @Before
@Before is used to execute a set of preconditions before executing each test case. @Before will be executed as many times as the number of test case in the program.
  • @After
@After is used to execute a set of conditions after executing each test case. @After is mainly used to reset some variables after the execution of the test case. @After will also execute as many times as the number of test case in the program.
  • @AfterClass
@AfterClass is executed after executing all the test cases in the Test Case class. @AfterClass is also executed once in the program.
  • @Test
@Test annotation is used for different types of test cases. When the expected result is an exception, we can give an additional parameter to @Test known as "expected = ". The function assertEquals includes the expected result and the input passed to the method.

In this program 4 different test cases are used. 
  1. The first one is to test a String with positive value.
  2. String with negative value.
  3. String 0
  4. Invalid String which throws an exception.
The order of execution of the program is as follows:

SetUp BeforeClass
SetUp
tearDown
SetUp
tearDown
SetUp
tearDown
SetUp
tearDown
tearDown AfterClass

The number of test case will vary according to the complexity of the program. And there is no limit to the number of test cases. As the number of test case increases and gives successful output, the more reliable the program will be.

If all the test cases are satisfied corresponding green signs will be shown (Eclipse). If any test case file, the failure of the test case will be shown.

I hope the blogpost is clear to everyone. Please comment below if you have any doubts. Comments and suggestions are always welcome.

Thank you for reading the post. !!!

References


  1. Wikipedia

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++