Friday, 24 August 2012

Junit: java unit testing framework with examples

java unit testing or junit

Junit is popular java unit testing framework. Junit is developed to unit test the piece of code.

What do you mean by unit testing? Unit testing is test code written for checking the runtime errors for each function/line of a java code. Junit can be used to regress the each small change so that large application should not give errors even adding small changes

Testing is piece of code written for checking the validity of actual business logic code like null check, and comparing actual and expected values.

Unit testing is a developer activity as part of software quality assurance process

junit can be coded in to test correctness of executions paths of java unit code
junit tests are coded by developers as part of software development life cycle.
Java unit testing can also be called as white box testing. Where as black box testing means functioaly of the system done by QA/testers.
After analysis and design phase, before writing code development, normal practise is to write the unit test code for the new functionally, then code new functionally, this way we find the all the issues for the new functionally

Junit is a open source testing framework and current version is 4.x. There are two types of test cases in junit. One is single test case, other is multiple test cases called Test suit

Junit can be shipped as jar file. You can download junit 4.x jar from junit site. Junit test can be executed in multiple following ways.
1 junit with maven plugin: - You need to define the junit dependency for your project where you are going to write unit test code. Here we will specify artefact name, groupId and version
2. junit with ant task:- Ant is provided different task to run the test case by configuring task in build.xml
Junit plugin in eclipse:-
Junit plugin should be installed to eclipse. Eclipse Galileo has default junit plugin installed.
3. junit eclipse:-
Junit examples


package org.cloudhadoop;

public class Calc {
public int add(int a, int b) {
return a + b;
}
}



package org.cloudhadoop;

import junit.framework.Assert;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.junit.Test;

public class CalcTest {
Calc calc = null;

@Before
public void setup() {
calc = new Calc();

}

@Test
public void testAdd() {
int a = 10;
int b = 20;
int actual = 30;
Assert.assertNotNull(calc);
int expected = c.add(a, b);
Assert.assertEquals(actual, expected);
}

@Test
public void testAddErorcase() {
int a = 10;
int b = 20;
int actual = 30;
Calc c = new Calc();
Assert.assertNotNull(c);
int expected = c.add(a, b);
Assert.assertEquals(actual, expected);
}

@After
public void tearDown() {

}

}





Java unit testing rules:-
All test classes name end with XXXXTest, for example class is Calculator, the Test class should be CalculatorTest
All test methods start with tesMethod and annoted the method as @Test

0 comments:

Post a Comment