Wednesday, March 5, 2008

Unit Testing with JUnit

JUnit is testing framework for Java. It comes inbuilt with eclipse.

This is the Very Simple application written by myself

public class Application {
public int addTowNumbers(int a, int b) {
if (Integer.toString(a).equals(null)
|| Integer.toString(b).equals(null)) {
return 0;
} else {
return a + b;

}
}
}

You can write Unit test for above application as follows.

import junit.framework.TestCase;

public class TestAPP extends TestCase {

public void test1() {
Application app = new Application();
assertEquals(app.addTowNumbers(0, 1) == 1, true);

}

public void test2() {
Application app = new Application();
assertEquals(app.addTowNumbers(4, 3) == 2, true);

}

}

Run above test case as JUnit test.