Hansel FAQ

General

When should I use coverage tests?
Does code coverage guarantee good test cases?

Troubleshooting

I think I achieved full code coverage, but Hansel still complains.
I'm unable to cover a piece of code. What should I do?
Can I run multiple coverage tests concurrently?
I get a java.lang.reflect.InvocationTargetException. What happened?
I get a java.lang.LinkageError/ java.lang.ClassCastException/ java.lang.NoSuchMethodException. What happened?
Why do some Exceptions seem to not get properly caught when using Hansel?

When should I use coverage tests?

There are many reasons to use coverage tests. Some of them are:

But always remember: Full code coverage does not guarantee, that the test case is a good test case. So its always better to write a good test case first and add code coverage testing later to make it even better.

Does code coverage guarantee good test cases?

No, they only help a little. You can call a method, without asserting any conditions afterwards. A test like this covers the method, but does not help in any way. Mutation Testing is an interesting approach to this problem.


I think I achieved full code coverage, but Hansel still complains.

Hansel does not work on the sourcecode, but it works on the bytecode. Sometimes basic block coverage of the java code is not equivalent to basic block coverage of the bytecode.

For example:

  public boolean not(boolean b {
    return !b;
  }
Is translated to:
  0:  iload_1
  1:  ifne  #8
  4:  iconst_1
  5:  goto  #9
  8:  iconst_0
  9:  ireturn
While the java code can be covered by one invocation of the method, two invocations are needed to cover the bytecode.

I'm unable to cover a piece of code. What should I do?

This is hard to answer this in general. If you are absolutely sure that the code can never be executed: remove it ;) Often mock objects can help to test code, that is hard to cover otherwise. If you find code, that (you think) cannot be covered and should not be changed, please tell me.


Can I run multiple coverage tests concurrently?

No, Hansel uses static fields to track code coverage. Running multiple coverage tests at the same time mixes the coverage results.


I get a java.lang.reflect.InvocationTargetException. What happened?

This was the way I thought, it would work. At least in one case it did not. Please tell me if you find a different solution to this problem.

java.lang.reflect.InvocationTargetException: java.lang.NoSuchMethodError
        at org.hansel.ModifyingClassLoader.(ModifyingClassLoader:35)
You are using an old version of BCEL. Please use either the version distributed with Hansel, or a version more recent than 2002/05/10 (Currently only available through CVS). If you are using the jar distributed with Hansel, but still get the exception, there might be an old jar file in:

Why do some Exceptions seem to not get properly caught when using Hansel?

If the Exception was loaded by an instrumented class loader and the class catching it was not (or visa versa), the Exception will not be caught. The solution is to add the Exception's package to the excluded packages list. See here for more details on hansel and classloaders.


I get a java.lang.LinkageError/ java.lang.ClassCastException/ java.lang.NoSuchMethodException. What happened?

You probably ran into a problem caused by using different classloaders. See here for more details on hansel and classloaders.