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

San*_*dal 5 java maven jacoco kotlin 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>
Run Code Online (Sandbox Code Playgroud)

但这不包括项目中kotlin代码的任何代码覆盖率。我只介绍Java代码。

该项目的结构为:

/projectName
  /src
    /main
      /java
      /kotlin
  pom.xml
Run Code Online (Sandbox Code Playgroud)

另外,在pom中,首先编译kotlin源(使用kotlin-maven-plugin),然后再编译Java源。

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

God*_*din 5

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

class InJava {
  void example() {
  }
}
Run Code Online (Sandbox Code Playgroud)

以下 src/main/kotlin/InKotlin.kt

class InKotlin {
  fun example() {
  }
}
Run Code Online (Sandbox Code Playgroud)

以下 src/test/java/ExampleTest.java

class InKotlin {
  fun example() {
  }
}
Run Code Online (Sandbox Code Playgroud)

pom.xml

public class ExampleTest {
  @org.junit.Test
  public void test() {
    new InJava().example();
    new InKotlin().example();
  }
}
Run Code Online (Sandbox Code Playgroud)

执行 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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

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

报告

其中包含Kotlin和Java代码。

  • 但是用 Kotlin 编写的测试呢……? (3认同)