Tuesday, March 14, 2006

JUnit intro

Following is the description about JUnit

Test suite
A TestSuite is a Composite of Tests. It runs a collection of test cases. Here is an example using the dynamic test definition.

TestSuite suite= new TestSuite();
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero"));

Alternatively, a TestSuite can extract the tests to be run automatically. To do so you pass the class of your TestCase class to the TestSuite constructor.

TestSuite suite= new TestSuite(MathTest.class);

This constructor creates a suite with all the methods starting with "test" that take no arguments.

Test Case
A test case defines the fixture to run multiple tests. To define a test case

1) implement a subclass of TestCase

2) define instance variables that store the state of the fixture

3) initialize the fixture state by overriding setUp

4) clean-up after a test by overriding tearDown.

5) For each test implement a method (starting with "test") which interacts with the fixture.Verify the expected results with assertions.(example assert methdos overloaded asserEquals,assertNotSame,assertTrue,assertNull)

AssertionFailedError is thrown when an assertion failed.

Running Test Cases
we can run the test cases in the following three ways

1)Once the methods are defined you can run them. The framework supports both a static type safe and more dynamic way to run a test. In the static way you override the runTest method and define the method to be invoked. A convenient way to do so is
with an anonymous inner class.

TestCase test= new MathTest("add") {
public void runTest() {
testAdd();
}
};
test.run();

2)The dynamic way uses reflection to implement runTest. It dynamically finds and invokes a method. In this case the name of the test case has to correspond to the test method to be run.

TestCase test = new MathTest("testAdd");
test.run();

3)The tests to be run can be collected into a TestSuite. JUnit provides different test runners which can run a test suite and collect the results. A test runner either expects a static method suite as the entry point to get a test to run or it will extract the suite automatically.

public static Test suite() {
suite.addTest(new MathTest("testAdd"));
suite.addTest(new MathTest("testDivideByZero"));
return suite;
}

small example
package testapp;
public class Example {
public boolean testOne(String str){
if(str.equalsIgnoreCase("yes")){
return true;
}else{
return false;
}
}
public boolean testTwo(int i,int j){
if(i>j){
return true;
}else{
return false;
}
}
}

package testapp;

import junit.framework.*;

public class ExampleTest extends TestCase {

Example instance;

public ExampleTest(String testName) {
super(testName);
}
protected void setUp() throws Exception {
instance = new Example();
}
protected void tearDown() throws Exception {
instance = null;
}
public static Test suite() {
//TestSuite suite = new TestSuite(ExampleTest.class);
// eihter use the above line or the following three lines
TestSuite suite = new TestSuite();
suite.addTest(new ExampleTest("testOne"));
suite.addTest(new ExampleTest("testTwo"));
return suite;
}
public void testOne() {
String str = "";
boolean expResult = true;
boolean result = instance.testOne(str);
assertEquals(expResult, result);
}
public void testTwo() {
int i = 0;
int j = 0;
boolean expResult = true;
boolean result = instance.testTwo(i, j);
assertEquals(expResult, result);
}
}

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]