Jacoco maven插件使用Exceptions-java.lang.IllegalStateException阻塞控制台:class已经过检测

时间:2017-08-08 06:08:42

标签: java powermock instrumentation jacoco-maven-plugin

我正在使用Jacoco插件处理声纳代码覆盖并使用power mock mockito组合来编写JUnit测试用例,而当我运行mvn clean install但是控制台显示非常长的堆栈跟踪链时,所有这些都很顺利。对于项目中使用的许多课程来说,它仍然存在,并且非常恼人, 异常跟踪类似于下面的

java.lang.instrument.IllegalClassFormatException: Error while instrumenting class..
Caused by: java.io.IOException: Error while instrumenting class.
Caused by: java.lang.IllegalStateException: Class <class-name> is already instrumented.

我通过以下链接寻求解决方案,但无法找到出路: -

https://github.com/jacoco/jacoco/issues/32

链接显示“错误表明为同一进程配置了两个JaCoCo代理,看起来像surefire-report-plugin的激活会导致加倍代理,这不是特例,但测试JVM的配置错误“在我的情况下,它甚至不是加倍的代理,我猜我的argLine只设置了一个jacoco代理。

在我的情况下,argLine设置为低于 -

javaagent:C:\\Users\\user\\.m2\\repository\\org\\jacoco\\org.jacoco.agent\\0.7.6.201602180812\\org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=C:Users\\user\\git\\package\\target\\jacoco.exec

我的jacoco-sonar配置的pom.xml条目如下 -

<properties>
        <sonar.sources>src/main/java</sonar.sources>
        <sonar.tests>src/test/java</sonar.tests>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.jacoco.reportPaths>${project.basedir}/target/jacoco.exec</sonar.jacoco.reportPaths>
        <sonar.language>java</sonar.language>
        <sonar.binaries>${project.basedir}/target/classes</sonar.binaries>
        <sonar.inclusions>
            **/com/abc/service/impl/ABCServiceImpl.java,
            **/com/abc/dao/impl/ABCDAOImpl.java
        </sonar.inclusions>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20</version>
                <configuration>
                    <skipTests>false</skipTests>
                    <argLine>-Xmx1024m -XX:MaxPermSize=256m ${argLine}</argLine>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.6.201602180812</version>
                <executions>
                 <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                </execution> 
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

注意:如果我删除'default-instrument'和'default-restore-instrumented-classes'的执行元素,那么异常跟踪不会出现,但我在声纳上得到覆盖率= 0.0%仪表板,使用这两个执行元素为我提供了正确的代码覆盖,但在控制台上有很长的堆栈跟踪。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

根据我对这项研究的见解: 也可以将离线检测的类与JaCoCo Java代理一起使用。在这种情况下,配置取自代理选项。必须以排除预先检测的类的方式配置代理,例如, “排除= * ”。否则,如果代理在您的情况下再次对这些类进行检测,则会在控制台上产生错误消息。

代理jacocoagent.jar是JaCoCo发行版的一部分,包含所有必需的依赖项。可以使用以下JVM选项激活Java代理:

-javaagent:[yourpath /] jacocoagent.jar = [选项1] = [VALUE1],[选项2] = [值]

对于JaCoCo代理选项,请考虑以下链接: http://www.jacoco.org/jacoco/trunk/doc/agent.html

答案 1 :(得分:0)

我使用mockito而不是powermock,但我认为与声纳的使用没有任何区别。我使用Sonar Maven Scanner进行分析,它就像一个魅力。

我的pom.xml包含以下属性

<properties>
    <project.source.version>1.8</project.source.version>
    <project.target.version>${project.source.version}</project.target.version>
    <sonar.scanner.version>3.3.0.603</sonar.scanner.version>
    <jacoco.maven.version>0.7.9</jacoco.maven.version>
    <sonar.projectName>MyProject</sonar.projectName>
    <sonar.projectKey>my-project-key</sonar.projectKey>
    <sonar.language>java</sonar.language>
</properties>   

并且插件配置如下

<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>${project.source.version}</source>
                <target>${project.target.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>${sonar.scanner.version}</version>
        </plugin>

        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.maven.version}</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>

您可以使用

运行分析
mvn clean install sonar:sonar

这将生成一个默认的exec文件,该文件位于target / jacoco.exec,将由您当地的sonarqube服务器使用,并且还包含覆盖范围。

更新回答

用powermock测试它。我使用了powermock jacoco-offline example并稍稍调整了一下。

然后是pom.xml

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“&GT;     4.0.0

<groupId>org.powermock</groupId>
<artifactId>jacoco-offline</artifactId>
<version>1.0.0-SNAPSHOT</version>

<name>JaCoCo Offline with PowerMock</name>
<description>
    Example how to get code coverage with PowerMock
</description>

<properties>
    <jacoco.version>0.7.9</jacoco.version>
    <sonar.scanner.version>3.3.0.603</sonar.scanner.version>
    <!-- Used to locate the profile specific configuration file. -->
    <build.profile.id>dev</build.profile.id>

    <jacoco.it.execution.data.file>${project.build.directory}/coverage-reports/jacoco-it.exec</jacoco.it.execution.data.file>
    <jacoco.ut.execution.data.file>${project.build.directory}/coverage-reports/jacoco-ut.exec</jacoco.ut.execution.data.file>

    <jdk.version>1.8</jdk.version>

    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!-- Only unit tests are run by default. -->
    <skip.unit.tests>false</skip.unit.tests>

    <powermock.version>1.7.0</powermock.version>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <mockito1.version>1.10.19</mockito1.version>
    <mockito2.version>2.8.9</mockito2.version>
    <easymock.version>3.4</easymock.version>
    <assertj-core.version>3.8.0</assertj-core.version>
    <junit.version>4.12</junit.version>
    <spring.version>3.0.5.RELEASE</spring.version>
    <commons-lang3.version>3.4</commons-lang3.version>

     <sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>

</properties>

<dependencies>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>${assertj-core.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
    </dependency>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.agent</artifactId>
        <version>${jacoco.version}</version>
        <classifier>runtime</classifier>
    </dependency>
    <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-core</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito-common</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-support</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-classloading-xstream</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4-rule-agent</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-easymock</artifactId>
            <version>${powermock.version}</version>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-testng</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>${powermock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>${easymock.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>${mockito1.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>${commons-lang3.version}</version>
        </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>${jdk.version}</source>
                <target>${jdk.version}</target>
                <encoding>${project.build.sourceEncoding}</encoding>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <executions>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${project.build.directory}/coverage.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${project.build.directory}/coverage.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.sonarsource.scanner.maven</groupId>
            <artifactId>sonar-maven-plugin</artifactId>
            <version>${sonar.scanner.version}</version>
        </plugin>
    </plugins>
</build>

这一切都是基本的,你是对的,我也错过了报道。运行mvn clean install后,您可以在目标文件夹中看到 coverage.exec 。默认情况下,声纳正在寻找一个jacoco.exec文件。

您只需添加

即可调整此行为
<sonar.jacoco.reportPaths>${project.basedir}/target/coverage.exec</sonar.jacoco.reportPaths>

到pom.xml中的properties部分。再次运行mvn clean install sonar:sonar后,我的本地声纳中出现了覆盖范围。希望这能帮助你更多...