Friday, April 22, 2011

JUnit Examples

Just JUnit

Calculator.java


package com.dev.junit.test;

public class Calculator {

public int add(int value1, int value2){
return value1+value2;
}
public int substract(int value1, int value2){
return value1-value2;
}
public int multiply(int value1, int value2){
return value1*value2;
}
public int devide(int value1, int value2){
return value1/value2;
}
}

Without Annotations

CalculatorTest.java

package com.dev.junit.test;

import junit.framework.TestCase;

public class CalculatorTest extends TestCase{

Calculator calc = null;
public void setUp(){
// to set up data
// runs before execution of every test case
calc = new Calculator();
}
public void testAdd() {
assertNotNull(calc);
int result = calc.add(4, 2);
assertEquals(6,result);
}
public void testSubstract() {
assertNotNull(calc);
int result = calc.substract(4, 2);
assertEquals(2,result);
}
public void testMultiply() {
assertNotNull(calc);
int result = calc.multiply(4, 2);
assertEquals(8,result);
}
public void testDevide() {
assertNotNull(calc);
int result = calc.devide(4, 2);
assertEquals(2,result);
}
public void tearDown(){
// to remove data
// runs after execution of every test case
}
}

With Annotations

CalculatorTest.java

package com.dev.junit.test;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class CalculatorTest {

Calculator calc = null;
@Before
public void setUp() throws Exception {
// to setup data
// runs before execution of every test case
calc = new Calculator();
}

@After
public void tearDown() throws Exception {
// to remove data
// runs after execution of every test case
}

@Test
public void testAdd() {
assertNotNull(calc);
int result = calc.add(4, 2);
assertEquals(6,result);
}

@Test
public void testSubstract() {
assertNotNull(calc);
int result = calc.substract(4, 2);
assertEquals(2,result);
}

@Test
public void testMultiply() {
assertNotNull(calc);
int result = calc.multiply(4, 2);
assertEquals(8,result);
}
@Test
public void testDevide() {
assertNotNull(calc);
int result = calc.devide(4, 2);
assertEquals(2,result);
}
}

Thursday, April 21, 2011

Unit Testing Frameworks

JUnit framework helps us to write and execute unit testing programs to test Java applications. We write a test case for each business method, and business logic. Sometimes we write many test cases for testing a single business method depending on the permutations and combinations to check that business logic. We also write test cases for constructors when business logic present in Constructor of a Class. We also write test cases for private methods when they have business logic. The Thumb Rule of unit test cases is: We write test case for testing business logic, where ever it present.

Unit testing applications helps us to:
Ø  To provide high-quality software.
Ø  Improve the developer’s confidence and customer’s satisfaction.
Ø  Test very big applications and business logic in a single click.
Ø  Make sure that the applications to perform consistently in the same way.
Ø  Provide a proof of the application unit testing and metrics.

Junit is sufficient for writing Unit testing applications in Java. But for writing Unit testing applications in J2EE environment we have few other frameworks. We have very good frameworks in Java/J2EE for writing Junit test cases.

Below are few frameworks. These are the extensions to the JUnit framework.

v  Cactus :
For testing server side programs like Servlet, EJB etc., The tests are executed in the Container. Hence the tests executed while server running.

v  JMock:
This framework supports TDD(Test-Driven-Development) for writing java code with mock objects.

v  Shale:
Shale framework provides mock objects for testing server-side java components in absence of container. Shale provides mock object libraries and base classes for creating Junit test cases. This focused on making easy to write test cases for Servlet. JSF, ViewControllers.

v  EasyMock:
This is a mock framework which provides mock objects to create Junit test cases for given interfaces. This provides an easy way to use mock objects. Easy mock provides mock objects for any given interfaces.

This is a light weight framework for writing Unit testing applications in J2EE environment. It supports servlet, filters, jstl, EJB,JMS, JCA, JDBC.

v  DBUnit :
This is a mock framework which provides mock objects to test Database programs. This provides mock objects for Connection, Statement, ResultSet etc.