Except for the catch block, EMMA incorrectly marks every line of code below red.
TestUtils uses reflection to invoke methods, and verifies that expected exceptions are thrown. That's easier
than surrounding each call with a flag, try, catch, set of flag, and test of flag.
Why does EMMA mark these red? Obviously they executed, or the line of code marked green could never have been
reached.
Throwable caughtException = null;
final ClassThatThrows classThatThrows = new ClassThatThrows(false);
final ClassThatDoesNotThrow classThatDoesNotThrow = new ClassThatDoesNotThrow();
TestUtils.assertIsExpectedException(
new IllegalArgumentException(),
IllegalArgumentException.class);
TestUtils.assertIsExpectedException(
new IllegalArgumentException("COVERAGE"),
IllegalArgumentException.class, "COVERAGE");
TestUtils.assertIsExpectedException(
new IllegalArgumentException("COVERAGE"),
IllegalArgumentException.class, null);
TestUtils.assertIsExpectedException(
new IllegalArgumentException(),
IllegalArgumentException.class, null);
try
{
TestUtils.assertIsExpectedException(
new IllegalStateException(), IllegalArgumentException.class);
}
catch (final Throwable e)
{
caughtException = e; <<< THIS IS THE ONLY GREEN LINE
}
Screenshots are available here:
http://jimshowalter.blogspot.com/2009/08/selecting-coverage-tool.html
You can now download the code that is exhibiting this behavior: http://jimshowalter.blogspot.com/2009/09/test-utils-are-now-open-source.html.