EclEmma, Java8 and Lambda - no coverage on lambda expression

江枫思渺然 提交于 2020-01-21 14:40:23

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/25627582/eclemma-java8-and-lambda-no-coverage-on-lambda-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!