java - 使用 JaCoCO 时测试失败

标签 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>

完整的 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 */
  }

稍后在代码中 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.");
  }
}

所有测试都因抛出相同的异常而失败,这在添加 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

注意 JaCoCo 添加的 -javaagent 选项。如果您想查看 mvn clean test 发出的全部日志,可以在这里找到它们:https://pastebin.com/kUtLtyjw

最佳答案

使用 JaCoCo 时,您希望确保合成字段和方法不会由您自己的代码处理,因为 JaCoCo 使用它们来收集覆盖率统计信息。幸运的是 FieldMethod 都有 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();
}

关于java - 使用 JaCoCO 时测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49154559/

相关文章:

java.lang.NoSuchMethodError : javax. servlet.ServletContext.getVirtualServerName()Ljava/lang/String;

java - Apache James 安装问题 - java.lang.UnsupportedClassVersionError :

maven - Jenkins maven项目构建失败

file - Maven 使用相同版本的新文件进行更新

maven - Surefire 插件输出 : "WARNING: An illegal reflective access operation has occurred"

java - Firebase 远程配置 - android

java - 在 tabFragment View 中实现搜索栏

eclipse - 导入 Maven-GWT 项目时出错 ("No marketplace entries found to handle gwt-maven-plugin")

java - Android - Jacoco 代码覆盖忽略 Robolectric 测试