Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EclEmma, Java8 and Lambda - no coverage on lambda expression

I have a Java project under Eclipse Luna, with EclEmma 2.3.1.201405111647 (latest), which use Jacoco 0.7.1, which have support for Java 8 as stated in their changelog:

"Version 2.3.1 (2014/05/11)

Fixed ASM 5.0.1 dependency conflicts with new ASM bundles in Eclipse 4.4 (GitHub #83).
Upgrade to JaCoCo 0.7.1 for full Java 8 support.

I now have the following toString:

  @Override
  public String toString() {
    // [BLOCK0]
    if (0 == value) {
      return "0B";
    }
    // [BLOCK1]
    final MutableLong val = new MutableLong(value);
    final StringBuilder sb = new StringBuilder();
    // [BLOCK2]
    Arrays.asList(TERA_BYTES, GIGA_BYTES, MEGA_BYTES, KILO_BYTES, BYTES).forEach(unit -> {
      // [BLOCK3]
      long divider = unit.toBytes(1);
      long n = val.longValue() / divider;
      if (0 != n) {
        sb.append(n).append(unit.getUnitCharacter());
        val.subtract(n * divider);
      }
    });
    // [BLOCK4]
    return sb.toString();
  }

I won't put the Junit test, because I know it goes 100% coverage. I can prove it by moving the lamdba expression into a appendToString method, and remplace the forEach with a for-each for (V value : Iterable<V>).

The result is, when I do "Coverage as Junit Test", the following:

  • BLOCK0 is all green
  • BLOCK1 is all green
  • BLOCK2 is green, up to the forEach(unit -> {
  • BLOCK3 is white (as if it were ignored lines)
  • BLOCK4 is all green.

Can someone explain me why Jacoco can't detect coverage in lambda ?

like image 526
NoDataFound Avatar asked May 15 '26 17:05

NoDataFound


1 Answers

Lambda expression bodies are compiled into synthetic methods, but as far as I read, synthetic methods are unconditionally filtered out in the code coverage analysis.

By looking at the Change History of JaCoCo I see

Snapshot Build 0.7.2.201408210455 (2014/08/21)

Fixed Bugs

Do not ignore synthetic lambda methods to get code coverage for Java 8 lambda expressions (GitHub #232).

which seems to address your issue. Since you are using EclEmma 2.3.1 which is using JaCoCo version 0.7.1 you just need an update.

like image 199
Holger Avatar answered May 17 '26 05:05

Holger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!