Tuesday, September 30, 2008

Testing Experience with Emma and JUnit

Objective

To become get familiar with testing tools such as Emma and JUnit. Also fix the Stack project so that the Stack project has 100% of coverage.

Improve Coverage

Emma is powerful tool that shows how many percentage of the original source code tested for expected output. Even the Stack project has fixed in order to pass the CheckStyle, FindBugs, JUnit and PMD tools, it had only 73% of coverage for method, 69% of coverage for block, and 75% of coverage for line on EMMA Coverage Report. In order to improve coverage on the source code, users can click the link for each class and Emma shows uncovered parts in red block. All fixed source file can be downloaded from http://www2.hawaii.edu/~jeho/ICS413/stack-jeho-6.0.930.zip

Added Test Cases
For testing toString method in the Stack class returns expected output:

assertEquals("Testing stack toString", "[Stack [1, 2, 3]]",stack.toString());
For testing EmptyStackException method in the Stack class returns expected output:

@Test(expected = EmptyStackException.class)
public void testIllegalTop() throws EmptyStackException {
Stack stack = new Stack();
stack.top();
fail("Top of empty stack did not generate exception.");
}
For testing isEmpty in the ClearStack class returns expected boolean return value:

assertSame("Testing for isEmpty",true, stack.isEmpty());
assertSame("Testing for isEmpty when it is not empty", false, stack.isEmpty());
For testing getTop throws EmptyStackException:

@Test(expected = EmptyStackException.class)
public void testIllegalgetTop() throws EmptyStackException {
ClearStack stack = new ClearStack();
stack.getTop();
fail("Top of empty stack did not generate exception.");
}
Reflect on Coverage

Emma is very useful tool because it automatically produce a report that shows which method or block need to be tested. The report was well organized and easy to understand. I believe improving the coverage of the original Stack project to 100% actually improved the quality of the system because I had to write additional test cases, which is actually needed to improve the quality of the system. I had little problem that the eclipse did not recognize other classes when I tried to change code with eclipse. This problem solved by clicking project - clean button and change the Java Build Path from /build/ to /bin/ .

No comments: