带有@PrepareForTest的Powermock Mockito在JaCoCo maven插件中未提供正确的覆盖范围还有其他选择吗?

问题描述

虽然使用PowerMockMockito进行存根和测试私有方法,但如果我们像这样包含@PrepareForTest({ AccountClientService.class })的任何类,则AccountClientService.java内部的所有方法都不会显示任何覆盖范围。尝试了来自堆栈溢出建议的各种方法,例如即时运行,脱机工具以及@Rule public PowerMockRule rule = new PowerMockRule(),但还是没有运气。跟踪和错误的各种版本。我的POM如下所示:

4.0.0 com.codependent.jacocopower jacoco-powermock 0.0.1-SNAPSHOT

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
              <argLine>-javaagent:path\\to\\powermock\\powermock-module-javaagent\\2.0.2\\powermock-module-javaagent-2.0.2.jar</argLine>
         <!--       <useSystemClassloader>true</useSystemClassloader> -->
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>${jacoco.version}</version>
            <configuration>
                <append>true</append>
            </configuration>
            <executions>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/*test*</include>
                        </includes>
                    </configuration>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                    <configuration>
                        <includes>
                            <include>**/*test*</include>
                        </includes>
                    </configuration>
                </execution>
                <execution>
                    <id>Prepare-Jacoco</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <excludes>
                            <exclude>**/*test*</exclude>
                        </excludes>
                    </configuration>
                </execution>
                <execution>
                    <id>report</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

 <dependencies>
    <dependency>
        <groupId>org.jacoco</groupId>
        <artifactId>org.jacoco.agent</artifactId>
        <version>${jacoco.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.powermock</groupId>
        <artifactId>powermock-api-mockito</artifactId>
        <version>${powermock.version}</version>
        <scope>test</scope>
    </dependency>
     <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-junit4-rule-agent</artifactId>
      <version>1.6.4</version>
      <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>

</dependencies>

<properties>
    <powermock.version>1.6.4</powermock.version>
    <jacoco.version>0.7.5.201505241946</jacoco.version>
    <jacoco.outputDir>${project.basedir}/target/jacoco.exec</jacoco.outputDir>
    <!-- <argLine>-javaagent:path\\to\\org.jacoco.agent\\0.7.5.201505241946\\org.jacoco.agent-0.7.5.201505241946-runtime.jar=destfile=${basedir}\\target\\jacoco.exec</argLine>
 --></properties>

解决方法

按照下面的链接进行步骤。它开始工作。我已经删除了与JaCoCo和maven-surefire-plugin相关的所有插件,并像下面的测试文件中所示。

http://www.notonlyanecmplace.com/make-eclemma-test-coverage-work-with-powermock/

感谢上述博客的作者。