Tuesday, July 31, 2007

Some Words About JUnit Testing

Testing is not a very interesting thing sometimes. Some developers use standard output or debugger in order to test their classes. But there is another way...
In this article you’ll find the introduction into JUnit library. JUnit framework makes the process of test-writing much easier.
To show you the power of JUnit let me create the small class in Java and write some test cases for it. Consider the following code:
public class MathFunc {
private int variable;

public MathFunc() {
variable = 0;
}
public MathFunc(int var) {
variable = var;
}

public int getVariable() {
return variable;
}
public void setVariable(int variable) {
this.variable = variable;
}

public long factorial() {
long result = 1;
if (variable > 1) {
for (int i=1; i<=variable; i++)
result = result*i;
}
return result;
}

public long plus(int var) {
long result = variable + var;
return result;
}
}

At first, you have to create the class which extends junit.framework.TestCase. The proper constructor also must be defined. This constructor has to provide String parameter to the parent class’ constructor. At last, you have to write as many test methods as you want:

public class TestClass extends TestCase {
public TestClass(String testName) {
super(testName);
}

public void testFactorialNull() {
MathFunc math = new MathFunc();
assertTrue(math.factorial() == 1);
}

public void testFactorialPositive() {
MathFunc math = new MathFunc(5);
assertTrue(math.factorial() == 120);
}

public void testPlus() {
MathFunc math = new MathFunc(45);
assertTrue(math.plus(123) == 168);
}
}

Method assertTrue checks the parameter’s value for “true” result. So, we can compare the output value of some method to the expected value. Methods assertEquals, assertFalse, assertNull, assertNotNull, assertSame are also useful ones.
In order to combine few tests together you can use TestSuite class. Just add some tests with addTest method. Finally, you have to use TestRunner to execute tests. I prefer to use junit.textui.TestRunner (there are also graphical implementations - junit.swingui.TestRunner, junit.awtui.TestRunner). The main method of the test class looks like this:

public static void main(String[] args) {
TestRunner runner = new TestRunner();
TestSuite suite = new TestSuite();
suite.addTest(new TestClass(“testFactorialNull”));
suite.addTest(new TestClass(“testFactorialPositive”));
suite.addTest(new TestClass(“testPlus”));
runner.doRun(suite);
}

Output after execution:

Time: 0,02
OK (3 tests)

For the testing of more complex classes you can use methods setUp and tearDown. The first method initializes several instances of the specified class in order to use them in several test cases. The second method serves to release unused resources after tests have passed.
I hope this article will help you a little.



Technorati Tags: , , ,

2 Comments:

IT Efforts said...

Thank you. Check necessarily

Aldrin and Luanne Misquitta said...

Check out TestNG as well-
http://testng.org/doc/

-Luanne
http://thought-bytes.blogspot.com/