JaCoCo JavaコードカバレッジMavenの例
Jacoco
は、アプリケーションのコードカバレッジを測定するために使用されます。このチュートリアルでは、mavenでJacocoを設定する方法とJacocoを使用してコードカバレッジレポートを表示する方法を理解します。
使用技術:
1. EclipseのMaven Javaプロジェクトを作成する
1.1 EclipseでMavenプロジェクト `File→ New→ Project→ Maven Project`を作成し、単純なプロジェクトを作成して、次をクリックします
1.2グループIDを入力
2.プロジェクトの構成
プロジェクトには次のコンポーネントがあります
-
サンプル算術演算クラス
-
サンプル算術演算JUnitテストクラス
-
JunitとJacocoの依存関係を持つ
pom.xml
3. pom.xml
`pom.xml`のようにJunitとJacocoを設定します。
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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.techfou</groupId> <artifactId>MathOperations</artifactId> <version>0.0.1-SNAPSHOT</version> <name>Mathematical Operations</name> <properties> <jacoco.version>0.7.5.201505241946</jacoco.version> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <skipMain>true</skipMain> <skip>true</skip> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>${jacoco.version}</version> <executions> <execution> <id>prepare-agent</id> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> </execution> <execution> <id>post-unit-test</id> <phase>test</phase> <goals> <goal>report</goal> </goals> <configuration> <!-- Sets the path to the file which contains the execution data. --> <dataFile>target/jacoco.exec</dataFile> <!-- Sets the output directory for the code coverage report. --> <outputDirectory>target/jacoco-ut</outputDirectory> </configuration> </execution> </executions> <configuration> <systemPropertyVariables> <jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile> </systemPropertyVariables> </configuration> </plugin> </plugins> </build> </project>
4.サンプル算術演算クラス
このクラスのメソッドでは、2つの整数パラメータを受け取り、sumを返す ‘add’が作成されます。
ArithmeticOperations.java
package math.operation; public class ArithmeticOperations { public Integer add(Integer a, Integer b) { return a+b; } }
5.サンプル算術演算Junitテストクラス
テストケースは ‘add’メソッド用に作成されます。
ArithmeticOperationsTest.java
package math.operation; import org.junit.Test; import static org.junit.Assert.assertEquals; public class ArithmeticOperationsTest { @Test public void testAdd() { ArithmeticOperations operations = new ArithmeticOperations(); Integer actual = operations.add(2, 6); Integer expected = 8; assertEquals(expected, actual); } }
6.アプリケーションを実行する
プロジェクト – >実行 – > Mavenテストを右クリックします。 Jacocoの出力レポートはjacoco-utフォルダの下のターゲットディレクトリに生成されます
7.出力
7.1出力を見るには、ターゲットディレクトリに行き、ブラウザのjacoco-utフォルダからindex.htmlを開きます。 `ArithmeticOperations`クラスの全体的なレポートは以下の通りです
7.2上の図の各メソッドをクリックすると、詳細なレポートが表示されます。ここには、緑色の線が表示され、どの線が単体テストに含まれているかが示されます。
完了しました。
ソースコードをダウンロードする
ダウンロード:
jacoco-maven-example.zip
(2 KB)
参考文献
-
https://en.wikipedia.org/wiki/Java
Code
Coverage__Tools[ウィキペディア –
Javaコードカバレッジツール]。
http://www.eclemma.org/jacoco/trunk/doc/maven.html
[JaCoCo – Maven
プラグイン – EclEmma]
リンク://タグ/コードカバレッジ/[コードカバレッジ]リンク://タグ/jacoco/[jacoco]