当前位置: 首页 > 知识库问答 >
问题:

Jacoco在Kotlin的测试中没有发现Kotlin文件在Kotlin和Java maven项目中的覆盖率

竺翰海
2023-03-14

我有静态编程语言和Java源文件夹和静态编程语言的项目,Java测试源文件夹。但是当我试图检查覆盖率时,Jacoco找不到静态编程语言类的覆盖率。

@Godin尝试应用您在类似问题上的答案中的build helper maven插件来添加更多源/测试目录,但结果是相同的“KotlinExample类行覆盖率为0.0…”

更新覆盖率仅在我从Java测试中覆盖Kotlin类时有效。但从科特林的测试来看,这是行不通的。

非常感谢您的帮助。

我的pom是

<modelVersion>4.0.0</modelVersion>

<properties>
    <main.version>0.0.1-SNAPSHOT</main.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.8</jdk.version>
    <kotlin.version>1.3.72</kotlin.version>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
    <jacoco.version>0.8.3</jacoco.version>
    <sonar.inclusions>src/main/**</sonar.inclusions>
    <sonar.exclusion.001>**/dto/**</sonar.exclusion.001>
    <sonar.exclusion.002>**/stub/**</sonar.exclusion.002>
    <sonar.skip>true</sonar.skip>
</properties>

<dependencies>
</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                    </manifestEntries>
                </archive>
                <webResources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                        <excludes>
                            <exclude>env.properties</exclude>
                        </excludes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <configuration>
                <jvmTarget>${jdk.version}</jvmTarget>
                <compilerPlugins>
                    <plugin>spring</plugin>
                </compilerPlugins>
            </configuration>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                   <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${basedir}/src/test/kotlin</sourceDir>
                            <sourceDir>${basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>java-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>${sonar.exclusion.001}</exclude>
                    <exclude>${sonar.exclusion.002}</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/coverage-reports/jacoco-ut.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-ut.exec</dataFile>
                        <outputDirectory>${basedir}/target/jacoco-ut-report</outputDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>check</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-ut.exec</dataFile>
                        <rules>
                            <rule>
                                <element>CLASS</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.8</minimum>
                                    </limit>
                                    <limit>
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.8</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

共有2个答案

祁博涛
2023-03-14

试试看:

<modelVersion>4.0.0</modelVersion>

<properties>
    <main.version>0.0.1-SNAPSHOT</main.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <jdk.version>1.8</jdk.version>
    <kotlin.version>1.3.72</kotlin.version>
    <kotlin.compiler.incremental>true</kotlin.compiler.incremental>
    <jacoco.version>0.8.3</jacoco.version>
    <sonar.inclusions>src/main/**</sonar.inclusions>
    <sonar.exclusion.001>**/dto/**</sonar.exclusion.001>
    <sonar.exclusion.002>**/stub/**</sonar.exclusion.002>
    <sonar.skip>true</sonar.skip>
</properties>

<dependencies>
</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>*</include>
            </includes>
        </resource>
    </resources>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                    </manifest>
                    <manifestEntries>
                    </manifestEntries>
                </archive>
                <webResources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <targetPath>WEB-INF/classes</targetPath>
                        <excludes>
                            <exclude>env.properties</exclude>
                        </excludes>
                    </resource>
                </webResources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>
            <configuration>
                <jvmTarget>${jdk.version}</jvmTarget>
                <compilerPlugins>
                    <plugin>spring</plugin>
                </compilerPlugins>
            </configuration>
            <executions>
                <execution>
                    <id>compile</id>
                    <phase>compile</phase>
                   <goals>
                        <goal>compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
                            <sourceDir>${project.basedir}/src/main/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                    <configuration>
                        <sourceDirs>
                            <sourceDir>${basedir}/src/test/kotlin</sourceDir>
                            <sourceDir>${basedir}/src/test/java</sourceDir>
                        </sourceDirs>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <artifactId>kotlin-maven-allopen</artifactId>
                    <version>${kotlin.version}</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <executions>
                <execution>
                    <id>default-compile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>default-testCompile</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <id>java-compile</id>
                    <phase>compile</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>java-testCompile</id>
                    <phase>test-compile</phase>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>${sonar.exclusion.001}</exclude>
                    <exclude>${sonar.exclusion.002}</exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <id>pre-unit-test</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/coverage-reports/jacoco-ut.exec</destFile>
                    </configuration>
                </execution>
                <execution>
                    <id>post-unit-test</id>
                    <phase>test</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-ut.exec</dataFile>
                        <outputDirectory>${basedir}/target/jacoco-ut-report</outputDirectory>
                    </configuration>
                </execution>
                <execution>
                    <id>check</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-ut.exec</dataFile>
                        <rules>
                            <rule>
                                <element>CLASS</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.4</minimum>
                                    </limit>
                                    <limit>
                                        <counter>BRANCH</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.4</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
白坚壁
2023-03-14

如果您使用的是0.8.3或类似版本的jacoco maven插件,请确保定义了数据文件,并对插件使用以下配置。

<execution>
  <id>jacoco-check</id>
  <goals>
    <goal>check</goal>
  </goals>
  <configuration>
    <dataFile>${project.build.directory}/coverage-reports/jacoco.exec</dataFile>
    <rules>
      <rule>
        <element>BUNDLE</element>
        <limits>
          <limit>
            <counter>INSTRUCTION</counter>
            <value>COVEREDRATIO</value>
            <minimum>0.17</minimum>
          </limit>
        </limits>
      </rule>
    </rules>
  </configuration>
</execution>

该代码确保了17%的总体覆盖率。

请使用验证

mvn清理验证

命令并将其添加到配置清单中

<configuration>
      <excludes>
        <exclude>META-INF/**</exclude>
      </excludes>
</configuration>
 类似资料:
  • 我们希望将Gradle与静态编程语言和Jacoco(JUnit 5)结合使用来生成代码覆盖率报告。 我们的项目目录树如下所示: < code>project/{src,test}/main/kotlin... 我们的build.gradle文件如下所示: 通过这种配置,Jacoco生成了一个html报告,并将其放在下。但是,它显示了0%的覆盖率。情况不应该是这样,因为我有一个测试用例,它为项目中的

  • 我使用Jacoco进行单元测试代码覆盖。Jacoco生成的报告显示,在我的Kotlin代码中很少遗漏分支。我注意到,根据Jacoco的说法,协程代码和它之后的代码没有被适当地覆盖。我不确定这是因为协程还是其他原因。在用IntelliJ代码覆盖率运行我的单元测试时,我的Kotlin类显示了100%的覆盖率。 我不知道为什么Jacoco显示的覆盖率较低。我已经使用Spock(Groovy)编写了单元测

  • 我正在设置通过SonarQube服务器分析项目。使用的工具集是: Kotlin 1.3.61 Gradle 6.0.1 Jacoco 0.7.9 SonarQube 7.5 SonnarQube Gradle插件2.7 问题是,我在SonarQube中有0.0%的覆盖率,但同时我有一份格式良好的jacoco测试覆盖率报告。以下是中的片段: 在执行< code >期间,我有下一个日志条目。/grad

  • 我想展示用Gradle 6.0构建的多项目Spring boot应用程序的测试覆盖范围。我们目前使用JUnit5。 SonarQube中的测试覆盖率为0%,尽管存在一些首次测试。 顶层项目(https://github.com/OpenReqEU/eclipse-plugin-vogella/blob/master/server/build.gradle)中的build.gradle文件有以下输入

  • 我正在做一个静态编程语言Java混合项目。 Gradle版本和插件最近分别升级到3.1.3和4.4。 现在,在编译时,我在一些Kotlin文件中发现了一个错误“未解析引用”。 例如:- 在上面的代码得到编译错误在android. r.id.home作为未解决的参考。 也在 在查找“removeOnGlobalLayoutListener”时获取“unresolved reference”错误 这件

  • 在Android应用程序中,将java类转换为kotlin,jacoco覆盖率开始显示编译器生成函数的0覆盖率,这是不可访问的。其他的在报告中似乎很好。 如何使kotlin和jacoco覆盖中字符串枚举的引用类型可测试 java代码: kotlin反编译了java代码: