无法从jacoco netbeans获得任何代码覆盖率

时间:2019-06-13 21:53:15

标签: java maven jacoco

我正在做一个大学项目,我需要覆盖本地代码以查看是否正确完成了测试。 但是,netbeans控制台保留

--- jacoco-maven-plugin:0.7.7.201606060606:报告(报告)@ ClientServicesProvider- 由于缺少执行数据文件,跳过JaCoCo执行。

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!--
    In this file, DO NOT EDIT any of following elements
    -->
    <groupId>com.mycompany</groupId>
    <artifactId>ClientServicesProvider</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <mainClass>com.mycompany.tp3.MainApp</mainClass>
    </properties>

    <dependencies>
        <!-- Students can only add new dependencies to this section -->

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-core</artifactId>
            <version>2.6.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.xmlunit</groupId>
            <artifactId>xmlunit-matchers</artifactId>
            <version>2.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jfoenix</groupId>
            <artifactId>jfoenix</artifactId>
            <version>8.0.8</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
            <version>2.44.0</version>
        </dependency>
        <dependency>
            <groupId>com.opera</groupId>
            <artifactId>operadriver</artifactId>
            <scope>test</scope>
            <version>1.5</version>
            <exclusions>
                <exclusion>
                    <groupId>org.seleniumhq.selenium</groupId>
                    <artifactId>selenium-remote-driver</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Required for compiling the project usign maven -->
            <plugin><!-- Compiler configuration-->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>

                    <showWarnings>true</showWarnings>   <!-- Needs this -->



                    <!-- <encoding>${project.build.sourceEncoding}</encoding> -->

                </configuration>
            </plugin>



            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>

                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/../bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>${mainClass}</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.build.finalName}.jar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>exec</goal>                            
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <commandlineArgs>${runfx.args}</commandlineArgs>
                        </configuration>
                    </execution>
                </executions>  
            </plugin>

            <!-- Required for running unit tests -->
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <!-- new configuration needed for coverage per test -->
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>org.sonar.java.jacoco.JUnitListener</value>
                        </property>
                    </properties>

                </configuration>
                <dependencies>
                    <!-- This dependency must be included, otherwise Maven Surefire will not recognise the test cases -->
                    <dependency>
                        <groupId>org.junit.platform</groupId>
                        <artifactId>junit-platform-surefire-provider</artifactId>
                        <version>1.2.0</version>
                    </dependency>
                </dependencies>

            </plugin>

            <!-- Required for generating coverage report -->
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.7.201606060606</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>


            <!-- Required for generating PIT Mutation reports -->
            <plugin>
                <groupId>org.pitest</groupId>
                <artifactId>pitest-maven</artifactId>
                <version>1.4.3</version>
                <dependencies>
                    <dependency> <!-- Only required because PITest does not work with JUnit5 without it -->
                        <groupId>org.pitest</groupId>
                        <artifactId>pitest-junit5-plugin</artifactId>
                        <version>0.8</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <!--<timestampedReports>false</timestampedReports>-->
                    <!--<inScopeClasses>
                        <param>com.mycompany.*</param>
                        <param>lapr.project.model.*</param>
                        <param>lapr.project.utils.*</param>
                    </inScopeClasses>-->
                    <targetClasses>
                        <param>com.mycompany.criagajos3000.*</param>
                        <param>com.mycompany.tp3.*</param>
                        <param>lapr.project.autorizacao.*</param>
                        <param>lapr.project.gpsd.controller.*</param>
                        <param>lapr.project.gpsd.model.*</param>

                    </targetClasses>
                    <targetTests>
                        <param>com.mycompany.criagajos3000.*</param>
                        <param>com.mycompany.tp3.*</param>
                        <param>lapr.project.autorizacao.*</param>
                        <param>lapr.project.gpsd.controller.*</param>
                        <param>lapr.project.gpsd.model.*</param>
                    </targetTests>
                    <outputFormats>
                        <outputFormat>XML</outputFormat>
                        <outputFormat>HTML</outputFormat>
                    </outputFormats>
                    <!--<verbose>true</verbose>-->
                </configuration>
            </plugin>

            <!-- Build an executable JAR -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:0)

您的pom.xml包含

                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>

但是在http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html引用JaCoCo文档:

  

如果您的项目已经定义了用于执行测试的VM参数,请确保它们将包含JaCoCo定义的属性。

     

在使用maven-surefire-plugin的情况下,执行此操作的方法之一是使用语法进行后期属性评估:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>@{argLine} -your -extra -arguments</argLine>
  </configuration>
</plugin>
     

另一种方法是将“ argLine”定义为Maven属性,而不是作为maven-surefire-plugin配置的一部分:

<properties>
  <argLine>-your -extra -arguments</argLine>
</properties>
...
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <!-- no argLine here -->
  </configuration>
</plugin>

因此,将argLine定义为属性:

    <build>
      <properties>
        <argLine>-Dfile.encoding=UTF-8</argLine>
      </properties>
      ...
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.1</version>
        <configuration>
          <!-- no argLine here -->

与您的问题无关,但是我也建议您使用最新的JaCoCo版本,即今天的0.8.4,而不是3年的旧版本0.7.7