JaCoCo makes tests fail

chris
2012-06-22
2013-04-24
  • chris
    chris
    2012-06-22

    Hi,

    I'm in the process of adding JaCoCo to my tycho-based build. However, JaCoCo makes some of my tests fail due to a NPE. I added a couple of System.out.println() to figure out what's going on, and here's the content of my strings:

    operator: bool:eq(string,string)
    signatureName: string
    methodName: $jacocoInit

    methodName looks a bit suspicious :-) and indeed causes the NPE, since the methodName is used as key to look up some object in a map.

    Any idea what's going on here?

    Thanks in advance,
    Christian

     
  • Hi,

    what does the output "operator", "signatureName" mean? Is this part of your application/framework?

    JaCoCo in fact adds methods and fields o instrumented classes. These are flagged as synthetic and should therefore be ignored by frameworks.

    Best regards,
    -marc

     
  • chris
    chris
    2012-06-22

    Hi,

    wow, thanks for the fast reply!

    Here's the code which produces the output - it's a constructor of a static private class.

            Operation(AlgebraRegister register, Algebra<?> algebra, Method method) {
                this.algebra = algebra;
                this.method = method;
                String signatureName = getSignatureName(algebra);
                System.out.println("signatureName: " + signatureName);
                System.out.println("methodName: " + method.getName());
                Operator operator =
                    operatorsMap.get(signatureName).get(method.getName());
                System.out.println("operator: " + operator);
                String returnTypeName = operator.getResultType();
                this.returnType = register.getImplementation(returnTypeName);
            }

    Hope this helps,
    Christian

     
  • chris
    chris
    2012-06-22

    Hi again,

    before you continue looking into this, give me a couple of hours - I just noticed that the code I'm talking about uses reflections (this is 3rd party code, but I fortunately have the sources). I will try to make it ignore methods whose name contains "jacoco" and see if this fixes the problem…

    Thanks anyways,
    Christian

     
  • chris
    chris
    2012-06-22

    Your comment about jacoco adding methods at runtime was very helpful - I indeed fixed the problem by ignoring the jacoco methods. In the following, the methodNames will (after the loop has finished) contain the names of valid (i.e., non-jacoco methods). In case there is a more clean way to identify jacoco methods than method.getName().toLowerCase().contains("jacoco"), please let me know.

            for (Method method : signatureMethods) {
                if (!method.getName().toLowerCase().contains("jacoco")) {
                    methodNames.add(method.getName());
                } else {
                    System.out.println("Ignored method with name "
                        + method.getName());
                }
            }

     
  • I strongly recommend to not hardcode any names in your code. Simply exclude synthetic methods:

      if (!method.isSynthetic()) {
        …
      }

    -marc

     
  • chris
    chris
    2012-06-22

    Ok, thanks, I changed my code accordingly, and it's working as you suggested…