使用Jacoco for Scala进行单元测试覆盖

时间:2013-03-27 10:59:23

标签: unit-testing scala jacoco

当我在Maven项目中使用以下结构时:

<plugins>
        <plugin>
            <groupId>org.scala-tools</groupId>
            <artifactId>maven-scala-plugin</artifactId>
            <version>2.15.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <args>
                    <arg>-g:line</arg>
                </args>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.5.8.201207111220</version>
            <executions>
                <execution>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

执行测试但未创建jacoco.exec文件。我尝试将此标记destFile添加到prepare-agent目标但未成功(未创建包含覆盖信息的文件)。

任何人都有任何形式使用maven和Jacoco来计算Scala单元测试的代码覆盖率?

1 个答案:

答案 0 :(得分:1)

Jacoco maven插件的工作取决于JDK,Scala和插件本身的版本。

刚刚添加了相同的配置并且工作正常:jacoco.exec和带有报告的site / jacoco都在目标目录中:https://github.com/plokhotnyuk/calculator/commit/e3a84db02dd942b0df71d4fa337df29512e213f6

使用最新(0.6.2.201302030002)版本的Jacoco maven插件时代理失败:

Caused by: java.lang.RuntimeException: Class java/util/UUID could not be instrumented.
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:138)
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:99)
    at org.jacoco.agent.rt.internal_5d10cad.PreMain.createRuntime(PreMain.java:51)
    at org.jacoco.agent.rt.internal_5d10cad.PreMain.premain(PreMain.java:43)
    ... 6 more
Caused by: java.lang.NoSuchFieldException: $jacocoAccess
    at java.lang.Class.getField(Class.java:1542)
    at org.jacoco.agent.rt.internal_5d10cad.core.runtime.ModifiedSystemClassRuntime.createFor(ModifiedSystemClassRuntime.java:136)
    ... 9 more
FATAL ERROR in native method: processing of -javaagent failed
Exception in thread "main" 

然后通过从JDK8切换到JDK7来解决失败问题。覆盖率报告数字未更改。

此外,在切换到Scala 2.10源代码行后,突出显示开始看起来不同:

Scala 2.9 enter image description here

Scala 2.10 enter image description here