ttk*_*203 5 java maven jacoco jacoco-maven-plugin
我正在尝试使用 JaCoCo 在这个项目中测量覆盖率:https : //github.com/square/retrofit
一切似乎都运行良好,但由于某种原因,一些曾经可以工作的测试在使用 JaCoCo 运行时代理时失败了。
这是我的(有趣的部分)pom.xml:
...
<plugins>
...
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
<configuration>
<!-- Sets the VM argument line used when unit tests are run. -->
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
...
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.0</version>
<executions>
<!--
Prepares the property pointing to the JaCoCo runtime agent which
is passed as VM argument when Maven the Surefire plugin is executed.
-->
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<!--
Sets the name of the property containing the settings
for JaCoCo runtime agent.
-->
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<!--
Ensures that the code coverage report for unit tests is created after
unit tests have been run.
-->
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
Run Code Online (Sandbox Code Playgroud)
完整的pom.xml可以在这里找到:https : //pastebin.com/HSKJpS3g
由于相同的原因,所有测试都失败了,让我举个例子。考虑Example在这个测试中声明的类:
@Test public void customMethodNoBody() {
class Example {
@HTTP(method = "CUSTOM1", path = "/foo")
Call<ResponseBody> method() {
return null;
}
}
/* Do some operations with the class Example */
}
Run Code Online (Sandbox Code Playgroud)
稍后将代码Exemple.class传递给TestingUtils.onlyMethod将引发异常的方法:
package retrofit2;
import java.lang.reflect.Method;
public final class TestingUtils {
public static Method onlyMethod(Class c) {
Method[] declaredMethods = c.getDeclaredMethods();
if (declaredMethods.length == 1) {
return declaredMethods[0];
}
throw new IllegalArgumentException("More than one method declared.");
}
}
Run Code Online (Sandbox Code Playgroud)
由于抛出相同的异常,所有测试都失败了,这在添加 JaCoCo 之前没有发生。为什么会这样?我该如何解决问题?
maven 用来运行测试的命令是这样的:
/usr/lib/jvm/java-8-oracle/jre/bin/java
-javaagent:/root/.m2/repository/org/jacoco/org.jacoco.agent/0.7.5.201505241946/org.jacoco.agent-0.7.5.201505241946-runtime.jar=destfile=/root/retrofit/retrofit/target/jacoco.exec
-jar /root/retrofit/retrofit/target/surefire/surefirebooter7714471086789859732.jar
/root/retrofit/retrofit/target/surefire/surefire2679778491836039056tmp
/root/retrofit/retrofit/target/surefire/surefire_02232907551688610770tmp
Run Code Online (Sandbox Code Playgroud)
注意-javaagentJaCoCo 添加的选项。如果您想查看由 发布的整个日志mvn clean test,可以在此处找到它们:https : //pastebin.com/kUtLtyjw
使用 JaCoCo 时,您要确保合成字段和方法不会由您自己的代码处理,因为 JaCoCo 使用它们来收集覆盖率统计信息。幸运的是,双方Field并Method有isSynthetic()方法。
public static Method onlyMethod(Class c) {
return Arrays.stream(c.getDeclaredMethods())
.filter(m -> !m.isSynthetic())
.reduce((a, b) -> {
throw new IllegalStateException("More than one method declared.");
})
.get();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2984 次 |
| 最近记录: |