java - 代码覆盖 Kotlin 和 Java 一起使用 Maven - Jacoco

标签 java maven kotlin jacoco jacoco-maven-plugin

我正在使用 Maven 插件通过 Maven 插件生成代码覆盖率,如下所示:

             <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

但这不包括项目中kotlin代码的任何代码覆盖率。我只是对 Java 代码进行覆盖。

项目的结构是:

/projectName
  /src
    /main
      /java
      /kotlin
  pom.xml

此外,在 pom 中,首先编译 kotlin 源代码(使用 kotlin-maven-plugin),然后编译 java 源代码。

如何为 Java 和 Kotlin 代码生成覆盖率?

最佳答案

给出以下 src/main/java/InJava.java

class InJava {
  void example() {
  }
}

跟随 src/main/kotlin/InKotlin.kt

class InKotlin {
  fun example() {
  }
}

跟随 src/test/java/ExampleTest.java

public class ExampleTest {
  @org.junit.Test
  public void test() {
    new InJava().example();
    new InKotlin().example();
  }
}

pom.xml

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>example</artifactId>
  <version>0.0.1-SNAPSHOT</version>

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

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <!--
            Add "src/main/kotlin" as additional source directory,
            so that kotlin-maven-plugin and jacoco-maven-plugin will look for files in it
            -->
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
              <goal>add-source</goal>
            </goals>
            <configuration>
              <sources>
                <source>${project.basedir}/src/main/kotlin</source>
              </sources>
            </configuration>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.jetbrains.kotlin</groupId>
        <artifactId>kotlin-maven-plugin</artifactId>
        <version>1.3.30</version>
        <executions>
          <execution>
            <id>compile</id>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.3</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

</project>

执行mvn verify

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.example:example >-------------------------
[INFO] Building example 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.3:prepare-agent (default) @ example ---
[INFO] argLine set to -javaagent:/Users/evgeny.mandrikov/.m2/repository/org/jacoco/org.jacoco.agent/0.8.3/org.jacoco.agent-0.8.3-runtime.jar=destfile=/private/tmp/j/target/jacoco.exec
[INFO]
[INFO] --- build-helper-maven-plugin:3.0.0:add-source (add-source) @ example ---
[INFO] Source directory: /private/tmp/j/src/main/kotlin added.
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /private/tmp/j/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /private/tmp/j/target/classes
[INFO]
[INFO] --- kotlin-maven-plugin:1.3.30:compile (compile) @ example ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /private/tmp/j/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /private/tmp/j/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ example ---
[INFO] Surefire report directory: /private/tmp/j/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running ExampleTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.046 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ example ---
[INFO] Building jar: /private/tmp/j/target/example-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- jacoco-maven-plugin:0.8.3:report (default) @ example ---
[INFO] Loading execution data file /private/tmp/j/target/jacoco.exec
[INFO] Analyzed bundle 'example' with 2 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.870 s
[INFO] Finished at: 2019-04-22T15:41:31+02:00
[INFO] ------------------------------------------------------------------------

编译 Kotlin 和 Java 代码,使用 JaCoCo Java 代理执行测试并在目录 target/site/jacoco 中生成以下报告

report

其中包含 Kotlin 和 Java 代码。

关于java - 代码覆盖 Kotlin 和 Java 一起使用 Maven - Jacoco,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55765833/

相关文章:

java - 如何从 Java 中的示例对象创建张量?

java - 生成空输出文件的 Map Reduce 作业

java - vaadin touchkit 示例应用程序 -- 没有 ivy.xml

kotlin - 解决 Kotlin MPP 中的第三方 cocoapod 依赖关系

android - 使用接口(interface)而不是类定义 Retrofit 返回类型

Java:如何使用 Stream API 按条件对实体进行分组?

java - 在 Python 中创建 Java DataInputStream 数据

java - Spark Java 和类路径

java - 如何在 child 的 pom 中将 jacoco 覆盖率设置为 60%?

intellij-idea - Kotlin @Serializable注释在IntelliJ中不起作用