sbt / sbt-jacoco

an sbt plugin for JaCoCo Code Coverage
https://scala-sbt.org/sbt-jacoco/
Eclipse Public License 1.0
123 stars 67 forks source link

Jacoco Reporting does not correctly handle Lambdas #312

Open mjackson-mim opened 2 months ago

mjackson-mim commented 2 months ago

Expected vs Actual Behavior

The generated report should handle Java lambdas, it does not seem to count the lines in the body as lines of code, nor highlight them when looking at the code. Instead, it counts a multi-line long lambda as one line that has been tested, even if only a portion of the lambda has been tested.

I ran a similar test using only Jacoco, and this did not seem to be an issue when using Jacoco 0.8.12.

Steps to Reproduce

Code to be tested:

public class Example {

    public boolean method() {
        return ((BooleanSupplier) () -> {
            System.out.println("\n>>>>>> Called from the lambda body.\n");
            if (true) {
                return true;
            } else {
                return false;
            }
        }).getAsBoolean();
    }
}

Test:

import org.junit.Assert;
import org.junit.Test;

public class ExampleTest {
    @Test
    public void test() {
        Assert.assertTrue(new Example().method());
    }
}

Run $ sbt jacoco (cleaning doesn't seem to do anything)

Console Output:

[info] welcome to sbt 1.10.0 (Azul Systems, Inc. Java 21.0.3)
[info] loading global plugins from C:\Users\mjackson\.sbt\1.0\plugins
[info] loading settings for project sbt_2-build from plugins.sbt ...
[info] loading project definition from C:\Users\mjackson\Documents\sbt-jacoco-lambda-example\sbt_2\project
[info] loading settings for project sbt_2 from build.sbt ...
[info] set current project to example (in build file:/C:/Users/mjackson/Documents/sbt-jacoco-lambda-example/sbt_2/)
[info] Instrumenting 1 classes to C:\Users\mjackson\Documents\sbt-jacoco-lambda-example\sbt_2\target\scala-2.12\jacoco\instrumented-classes

>>>>>> Called from the lambda body.

[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info]
[info] ------- Jacoco Coverage Report -------
[info]
[info] Lines: 100% (>= required 0.0%) covered, 0 of 3 missed, OK
[info] Instructions: 100% (>= required 0.0%) covered, 0 of 6 missed, OK
[info] Branches: 0% (>= required 0.0%) covered, 0 of 0 missed, OK
[info] Methods: 100% (>= required 0.0%) covered, 0 of 2 missed, OK
[info] Complexity: 100% (>= required 0.0%) covered, 0 of 2 missed, OK
[info] Class: 100% (>= required 0.0%) covered, 0 of 1 missed, OK
[info]
[info] Check C:\Users\mjackson\Documents\sbt-jacoco-lambda-example\sbt_2\target\scala-2.12\jacoco\report for detailed report
[info]
[success] Total time: 2 s, completed Jun 21, 2024, 3:48:44

JaCoCo Report:

Note that the number of lines tested in method() is 2, not 7 or 8.

image

image

Environment