Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco coverage for switch statement

I am working to get 100% code coverage for a library I am working on and I seem to have some issues with a switch statement and the coverage which I simply don't understand.

I am currently using Jacoco 0.7.2 because every newer version seems to break with Robolectrics.

I test a simple switch statement:

public enum Type {
    NONE, LEGACY, AKS
}

private static Class<?> getCipherClass(Type type) {
    switch (type) {
        case LEGACY:
            return CipherWrapperLegacy.class;
        case AKS:
            return CipherWrapperAks.class;
        default:
            return null;
    }
}

The test I wrote contains the following checks (I have to use reflection as the method is private):

final CipherWrapper instance = CipherWrapper.createInstance(mockContext, CipherWrapper.Type.LEGACY, ALIAS);
assertNotNull(instance);

Method getCipherMethod = TestUtils.makeMethodAccessible(CipherWrapper.class, "getCipherClass", CipherWrapper.Type.class);
assertNull(getCipherMethod.invoke(instance, CipherWrapper.Type.NONE));
assertEquals(CipherWrapperAks.class, getCipherMethod.invoke(instance, CipherWrapper.Type.AKS));
assertEquals(CipherWrapperLegacy.class, getCipherMethod.invoke(instance, CipherWrapper.Type.LEGACY));

The result is not what I have expected:

Jacoco code coverage result

The image is a bit confusing as the yellow line suggests that there is something missing. The green icon tells me that 3 of 3 branches are covered.

I also tested to extend the switch case with case NONE and a fall through but it didn't change anything.

The only thing I can do is to replace the switch with if/else and then I get 100% coverage.

Currently I have 98% coverage but I nothing is missed based on the overview: Jacoco overall coverage

like image 969
WarrenFaith Avatar asked Feb 09 '16 09:02

WarrenFaith


People also ask

How code coverage is calculated in JaCoCo?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100. 16) Where is the JaCoCo report generated Gradle?

Why is JaCoCo not showing coverage for some classes?

Why does the coverage report not show highlighted source code? Make sure the following prerequisites are fulfilled to get source code highlighting in JaCoCo coverage reports: Class files must be compiled with debug information to contain line numbers. Source files must be properly supplied at report generation time.

What is the difference between sonar and JaCoCo?

SonarQube has a broader approval, being mentioned in 163 company stacks & 271 developers stacks; compared to JaCoCo, which is listed in 5 company stacks and 11 developer stacks.

Is JaCoCo and EclEmma same?

JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years.


1 Answers

If the invoke method doesn't like you putting in an anonymous variable:

getCipherMethod.invoke(instance, (CipherWrapper.Type) null);

Then try it with a named variable:

CipherWrapper.Type nullType = null;
getCipherMethod.invoke(instance, nullType);

Also, you should check if the invocation exception is just wrapping an exception caused by invoking the method rather than an error with invocation itself.

like image 135
Graeme Avatar answered Sep 22 '22 08:09

Graeme