JUnit Tutorial | Testing Framework for Java

JUnit is the most famous unit test framework in Java programmers. However it is only suitable for pure unit testing, for integration test, you should go for TestNG instead.The java programmer can create test cases and test his/her own code.

It is one of the unit testing framework. Current version is junit 4.

JUnit promotes the idea of "first testing then coding", which emphasis on setting up the test data for a piece of code which can be tested first and then can be implemented . This approach is like "test a little, code a little, test a little, code a little..." which increases programmer productivity and stability of program code that reduces programmer stress and the time spent on debugging.

The org.junit package contains many interfaces and classes for junit testing such as Assert, Test, Before, After etc.


Features

  • JUnit is an open source framework which is used for writing & running tests.
  • Provides Annotation to identify the test methods.
  • Provides Assertions for testing expected results.
  • Provides Test runners for running tests.
  • JUnit tests allow you to write code faster which increasing quality
  • JUnit is elegantly simple. It is less complex & takes less time.
  • JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.
  • JUnit tests can be organized into test suites containing test cases and even other test suites.
  • Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.

What is a Unit Test Case ?

A Unit Test Case is a part of code which ensures that the another part of code (method) works as expected. To achieve those desired results quickly, test framework is required .JUnit is perfect unit test framework for java programming language.

Types of unit testing

There are two ways to perform unit testing:

  1. manual testing
  2. automated testing.

Manual Testing

If you execute the test cases manually without any tool support, it is known as manual testing. It is time consuming and less reliable.

Automated Testing

If you execute the test cases by tool support, it is known as automated testing. It is fast and more reliable.


Annotations for Junit testing

The Junit 4.x framework is annotation based, so let's see the annotations that can be used while writing the test cases.


Annotations for Junit testing

The Junit 4.x framework is annotation based, so let's see the annotations that can be used while writing the test cases.

@Test annotation specifies that method is the test method.

@Test(timeout=1000) annotation specifies that method will be failed if it takes longer than 1000 milliseconds (1 second).

@BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.

@Before annotation specifies that method will be invoked before each test.

@After annotation specifies that method will be invoked after each test.

@AfterClass annotation specifies that method will be invoked only once, after finishing all the tests.


Simple Example Of JUnit

This tutorial introduces the basic annotation supported in Junit 4.


 import org.junit.*;

import static org.junit.Assert.*;

import java.util.*;

 

/**

 * @author Mithilesh

 *

 */

public class JunitTest1 {

 

    private Collection collection;

 

    @BeforeClass

    public static void oneTimeSetUp() {

        // one-time initialization code   

    	System.out.println("@BeforeClass - oneTimeSetUp");

    }

 

    @AfterClass

    public static void oneTimeTearDown() {

        // one-time cleanup code

    	System.out.println("@AfterClass - oneTimeTearDown");

    }

 

    @Before

    public void setUp() {

        collection = new ArrayList();

        System.out.println("@Before - setUp");

    }

 

    @After

    public void tearDown() {

        collection.clear();

        System.out.println("@After - tearDown");

    }

 

    @Test

    public void testEmptyCollection() {

        assertTrue(collection.isEmpty());

        System.out.println("@Test - testEmptyCollection");

    }

 

    @Test

    public void testOneItemCollection() {

        collection.add("itemA");

        assertEquals(1, collection.size());

        System.out.println("@Test - testOneItemCollection");

    }

}


Output

	

@BeforeClass - oneTimeSetUp

@Before - setUp

@Test - testEmptyCollection

@After - tearDown

@Before - setUp

@Test - testOneItemCollection

@After - tearDown

@AfterClass - oneTimeTearDown