JUnit Sample Program

JUnit is a unit testing framework for the Java programming language. JUnit has been important in the development of test-driven development. JUnit provides a ready-made test harness for executing unit tests as well as an API for reporting the success or failure status of a test.

A Junit is a piece of code written by a developer that executes a specific functionality in the code to be tested. The percentage of code which is tested by unit tests is typically called test coverage.

A Junit targets a small unit of code, e.g., a method or a class, (local tests).

Junit s ensure that code works as intended. They are also very helpful to ensure that the code still works as intended in case you need to modify code for fixing a bug or extending functionality. Having a high test coverage of your code allows you to continue developing features without having to perform lots of manual tests.

JUnit test cases follow below rules:

  • Each unit test must run independently of all other unit tests.
  • Errors must be detected and reported test by test.
  • It must be easy to define which unit tests will run.

JUnit features:

  • Standard resource initialization and reclamation methods
  • Separate class loaders for each unit test to avoid side effects.
  • A variety of assert methods to check the results of your tests.
  • Alternate front-ends to display the results of your tests.
  • Integration with popular tools Ant, maven and popular IDEs like Eclipse, Jbuilder.

Junit Design Goals:

  • The framework must help us lower the cost of writing tests by reusing code.
  • The framework must help us create tests that retain value overtime.
  • Any class can be a test case and all test methods should have @Test annotation.
  • The test class does not need to extend any particular class.
  • All unit test methods to be public void and take no parameters.
  • Test methods to make Assert calls to validate the outcome.
  • Available annotation for test methods

  •     @BeforeClass - run before any test has been executed only once for the execution.
        @AfterClass - run after all the tests have been executed
        @Before - run before each test
        @After - run after each test

    Sample JUnit:
    public class Addition {
          public int add(int a,int b){
                return (a+b);
          }
    }
    

    Addition.class with add method, now will write test cases for the add method.
    package net.javaVillage;
    
    import static org.junit.Assert.assertEquals;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class AdditionTest {
    	private Addition addition;
    
    	/** * Initialization */
    	@Before
    	public void setUp() {
    		addition = new Addition();
    	}
    
    	/** * Test case for add method */
    	@Test
    	public void test() {
    		int i = addition.add(3, 7);
    		assertEquals(10, i);
    	}
    
    	/** * destroy the object */
    	@After
    	public void tearDown() {
    		addition = null;
    	}
    }
    
    Here I want to share some points regarding test cases
    1) As per Test case for add method is covered only positive scenario but we have to cover negative scenarios also. Negative scenario nothing but input is not valid so what is the expected output like that we have to cover all the possibility scenarios for the test case.

    2) Coming to the setUp with @Before and tearDown method with @After are for initializing object and destroying it. These are not mandatory, so without these methods also we can write test cases. But If we are not destroying object after execution of test, every object will occupy memory and will get OutOfMemoryException. So this is the correct procedure to implement these two methods.