使用JaCoCO时测试失败

时间:2018-03-07 14:41:06

标签: 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.xmlhttps://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

1 个答案:

答案 0 :(得分:1)

使用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();
}