我在Eclipse Luna下有一个Java项目,使用EclEmma 2.3.1.201405111647(最新版),它使用Jacoco 0.7.1,它支持Java 8,如更改日志:
版本2.3.1(2014/05) / 11)
修复了Eclipse 4.4(GitHub#83)中新ASM包的ASM 5.0.1依赖冲突。升级到JaCoCo 0.7.1以获得完整的Java 8支持。
我现在有以下toString:
@Override public String toString(){ // [ BLOCK0] if(0 == value){返回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] 返回sb.toString(); }我不会进行Junit测试,因为我知道它是100%覆盖。我可以通过将lamdba表达式移动到 appendToString 方法来证明这一点,并使用重新设置 forEach -each for(V值:Iterable< V>)。
结果是,当我做报道为Junit测试,如下:
- BLOCK0全为绿色
- BLOCK1全部为绿色
- BLOCK2为绿色,最高为 forEach(单位 - > {
- BLOCK3是白色的(好像被忽略了一行)
- BLOCK4全为绿色。
将Lambda表达式体编译成合成方法,但据我所知,合成方法无条件过滤掉了代码覆盖率分析。
通过查看 JaCoCo的变更历史我看到了
快照构建0.7.2.201408210455(2014/08/21)修正错误
不要忽略合成的lambda方法来获取Java 8 lambda表达式的代码覆盖率(GitHub#232)。
<这似乎解决了你的问题。由于您使用的是使用JaCoCo版本 0.7.1 的EclEmma 2.3.1 ,您只需要更新。
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 ?
解决方案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.
更多推荐
EclEmma,Java8和Lambda
发布评论