JUnit Introduction with Example

JUnit is a unit testing framework for the Java programming language and it became important in the development of test-driven development. Annotations - Given by Apache Foundation.

JUnit promotes the idea of testing for developers which emphasis on setting up the test data for a piece of code which can be tested first and can be implemented.

Features of JUnit

  • JUint is an open source framework which is used for writing test cases and executing them
  • It Provides Annotation support to identify the test methods
  • Provides Assertions for comparing actual and expected results
  • JUnit is very simple and easy to use and requires less time
  • JUnit provides the results after executing the results with green and red status for Pass and Failed test cases
  • Provides test runners for running tests

Earlier to JUnit
1)Developer has to perform unit testing for the program which he/she has developed to make sure every thing is working fine.

2)Before Junit framework, if developer want to do the unit testing then he needs to prepare developer test cases and it contains lot of test steps to do.

3)If developers executes all the test cases on to the application with developer test cases then that process is called as manual unit test case.

4) When Developer do the manual unit testing then he needs to spend lot of effort to prepare the test cases in the form of excel sheets and execute the test cases.

5) If the project is enhancement project, developer needs to spend more time on to the unit testing rather then coding to resolve the problem.

Junit is a unit testing f/w, by using this f/w we can do the unit testing. To write Junit test cases or junit classes, junit framework provides one class called “Testcase”
TestCase:-If we want to write our own test cases(or) Test classes by using Junit, then every test class should extends the Junit test case class.

Below is the basic example JUnit program:

import org.junit.*;
public class SampleTest{
    @BeforeClass
    public static void setUpClass() throws Exception {
    	System.out.println("Hello.. Im in before class method");
    }
    @Before
    public void setUp() throws Exception {
    	System.out.println("Hello.. im in Before test method");
    }
    @Test
    public void testcaseOne() {
    	System.out.println("Hello... Im in First test case");
    }
    @Test
    public void testcaseTwo() {
    	System.out.println("Hello... Im in second test case");
    }
    @Test
    public void testsaseThree() {
    	System.out.println("Hello... Im in third test case");
    }
    @After
    public void tearDown() throws Exception {
    	System.out.println("Hello.. im in After test method");   
    }
    @AfterClass
    public static void tearDownClass() throws Exception {
    	System.out.println("Hello.. Im in After class method");  
    }
}

Output for the Above Program will be:

Hello.. Im in before class method
Hello.. im in Before test method
Hello... Im in First test case
Hello.. im in After test method
Hello.. im in Before test method
Hello... Im in second test case
Hello.. im in After test method
Hello.. im in Before test method
Hello... Im in third test case
Hello.. im in After test method
Hello.. Im in After class method

Test Frameworks: 

Comments

Thanks for sharing

I need program of addition of two number and it is testing by junit framework..
earlisest response is highly appreciate.

Thanks for article.

can you pls tell how to test data from xml file for junit.
Means i want to perform data driven Junit testing using a xml file.

Regards,
Abhijit

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.