これは、なにをしたくて書いたもの?
MavenプロジェクトにJaCoCoを導入するメモ。
JaCoCo
JaCoCoはJavaのカバレッジを取得するライブラリーです。Webサイトはこちら。
EclEmma - JaCoCo Java Code Coverage Library
ドキュメントはこちら。
Mavenプロジェクト向けにはプラグインがあるので、こちらを導入します。
サンプルは次の2つがあります。
- 単体テスト用
- JUnitテスト実行時にカバレッジを取得し、カバレッジレポートを生成する
- カバレッジが60%を下回ったらビルドを失敗させる
- https://www.jacoco.org/jacoco/trunk/doc/examples/build/pom.xml
- 単体テスト+インテグレーションテスト用
使うゴールは主にこちらでしょう。
- 準備
- レポート生成
- JaCoCo - jacoco:report
- インテグレーションテストの場合はJaCoCo - jacoco:report-integration
- カバレッジが満たされているかチェック
ひとまず、簡単に導入してみます。
環境
今回の環境はこちら。
$ java --version openjdk 21.0.4 2024-07-16 OpenJDK Runtime Environment (build 21.0.4+7-Ubuntu-1ubuntu222.04) OpenJDK 64-Bit Server VM (build 21.0.4+7-Ubuntu-1ubuntu222.04, mixed mode, sharing) $ mvn --version Apache Maven 3.9.8 (36645f6c9b5079805ea5009217e36f2cffd34256) Maven home: $HOME/.sdkman/candidates/maven/current Java version: 21.0.4, vendor: Ubuntu, runtime: /usr/lib/jvm/java-21-openjdk-amd64 Default locale: ja_JP, platform encoding: UTF-8 OS name: "linux", version: "5.15.0-117-generic", arch: "amd64", family: "unix"
準備
Maven依存関係など。
<properties> <maven.compiler.release>21</maven.compiler.release> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> </properties> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.3</version> <scope>test</scope> </dependency> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.26.3</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.12</version> <executions> <!-- あとで --> </executions> </plugin> </plugins> </build>
テスト用のライブラリーと、JaCoCo Maven Pluginを導入。JaCoCo Maven Pluginのexecutions
の設定はあとで載せます。
サンプルコード
src/main/java/org/littlewings/jacoco/CalcService.java
package org.littlewings.jacoco; public class CalcService { public int plus(int a, int b) { return a + b; } public int minus(int a, int b) { return a - b; } }
テストコード。
src/test/java/org/littlewings/jacoco/CalcServiceTest.java
package org.littlewings.jacoco; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; class CalcServiceTest { @Test void plus() { CalcService calcService = new CalcService(); assertThat(calcService.plus(5, 3)).isEqualTo(8); } @Test void minus() { CalcService calcService = new CalcService(); assertThat(calcService.minus(8, 5)).isEqualTo(3); } }
確認。
$ mvn test
JaCoCo Maven Pluginの設定を行う
サンプルに習ってJaCoCo Maven Pluginの設定をします。
<plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.12</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
もとのサンプルはこちら。
https://www.jacoco.org/jacoco/trunk/doc/examples/build/pom.xml
テストを実行すると
$ mvn test
JaCoCoが組み込まれ
[INFO] --- jacoco:0.8.12:prepare-agent (prepare-agent) @ maven-jacoco-example --- [INFO] argLine set to -javaagent:$HOME.m2/repository/org/jacoco/org.jacoco.agent/0.8.12/org.jacoco.agent-0.8.12-runtime.jar=destfile=/path/to/target/jacoco.exec
target
ディレクトリ配下にjacoco.exec
というファイルが生成されます。
$ ll target/jacoco.exec -rw-rw-r-- 1 xxxxx xxxxx 26072 8月 4 18:08 target/jacoco.exec
あとはjacoco:report
ゴールを実行すると
$ mvn jacoco:report
生成されているjacoco.exec
を元にして
[INFO] --- jacoco:0.8.12:report (default-cli) @ maven-jacoco-example --- [INFO] Loading execution data file /path/to/target/jacoco.exec [INFO] Analyzed bundle 'maven-jacoco-example' with 1 classes
target/site/jacoco
にレポートが生成されます。
$ ll target/site/jacoco 合計 80 drwxrwxr-x 4 xxxxx xxxxx 4096 8月 4 18:10 ./ drwxrwxr-x 3 xxxxx xxxxx 4096 8月 4 18:10 ../ -rw-rw-r-- 1 xxxxx xxxxx 2906 8月 4 18:10 index.html drwxrwxr-x 2 xxxxx xxxxx 4096 8月 4 18:10 jacoco-resources/ -rw-rw-r-- 1 xxxxx xxxxx 51305 8月 4 18:10 jacoco-sessions.html -rw-rw-r-- 1 xxxxx xxxxx 256 8月 4 18:10 jacoco.csv -rw-rw-r-- 1 xxxxx xxxxx 2276 8月 4 18:10 jacoco.xml drwxrwxr-x 2 xxxxx xxxxx 4096 8月 4 18:10 org.littlewings.jacoco/
視覚的にはindex.html
を確認するのがよいでしょう。
テストを実行したらカバレッジレポートを生成する
テストを実行した後にmvn jacoco:report
と実行するのがめんどうな場合はjacoco:report
ゴールを指定しているところのphase
にtest
を
指定するとよいでしょう。
<execution> <id>report</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution>
これで、mvn test
と実行するとカバレッジレポートの生成まで行ってくれるようになります。
$ mvn test
おわりに
MavenプロジェクトにJaCoCoを導入してみました。
知っていればさっと終わるような内容だと思うのですが、個人的にちょっとメモしておきたかったので。