1. 程式人生 > >Maven工程配置代碼覆蓋工具Jacoco

Maven工程配置代碼覆蓋工具Jacoco

java redis dubbo spring

本篇博文我們將給出示例理解如何在Maven工程中配置Jacoco和如何使用Jacoco查看代碼覆蓋報告~


Jacoco是一個開源的Java代碼覆蓋率工具,Jacoco可以嵌入到Ant 、Maven中,並提供了EclEmma Eclipse插件,也可以使用JavaAgent技術監控Java程序。很多第三方的工具提供了對Jacoco的集成,如sonar、Jenkins等。


Maven工程


創建Maven工程


打開Eclipse,File->New->Project->Maven Project,新建一個Maven工程~

技術分享

更多資料共享請加入Java高並發高可用架構:632103578


點擊“Next”按鈕,然後填寫groupId和artifactId信息後點擊"Finish"按鈕即可~


groupId --> com.xxx.tutorial

artifactId --> jacoco-demo

技術分享


配置Jacoco


添加maven-complier-plugin

           <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.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>


添加jacoco-maven-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>
							<dataFile>target/jacoco.exec</dataFile>
							<outputDirectory>target/jacoco-ut</outputDirectory>
						</configuration>
					</execution>
				</executions>
				<configuration>
					<systemPropertyVariables>
						<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
					</systemPropertyVariables>
				</configuration>
			</plugin>


在這裏,我們將單元測試結果的輸出目錄確定為target/jacoco-ut目錄下~


完整的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.xxx.tutorial</groupId>
	<artifactId>jacoco-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<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.7</source>
					<target>1.7</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>
							<dataFile>target/jacoco.exec</dataFile>
							<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>

編寫代碼


Calculator.java

package com.xxx.tutorial;

/**
 * 
 * @author wangmengjun
 *
 */
public class Calculator {

	public int add(int a, int b) {
		return a + b;
	}

	public int sub(int a, int b) {
		return a - b;
	}
}


Calculator_Test.java

package com.xxx.tutorial;

import org.junit.Assert;
import org.junit.Test;

/**
 * 
 * @author wangmengjun
 *
 */
public class Calculator_Test {

	private Calculator instance = new Calculator();

	@Test
	public void testAdd() {
		int a = 10;
		int b = 20;
		int expected = 30;
		Assert.assertEquals(expected, instance.add(a, b));
	}

	@Test
	public void testSub() {
		int a = 10;
		int b = 20;
		int expected = -10;
		Assert.assertEquals(expected, instance.sub(a, b));
	}
}


代碼結構如下:

技術分享


運行並查看Jacoco報告


運行Maven test

技術分享


執行Maven test, 控制臺輸出如下結果:


技術分享

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building jacoco-demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:prepare-agent (prepare-agent) @ jacoco-demo ---
[INFO] argLine set to -javaagent:D:\\java_tools\\Reponsitories\\Maven\\org\\jacoco\\org.jacoco.agent\\0.7.5.201505241946\\org.jacoco.agent-0.7.5.201505241946-runtime.jar=destfile=F:\\JavaDeveloper\\workspaces\\SpringMVCDubboExample\\jacoco-demo\\target\\jacoco.exec
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ jacoco-demo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:compile (default-compile) @ jacoco-demo ---
[INFO] Not compiling main sources
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ jacoco-demo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.6.1:testCompile (default-testCompile) @ jacoco-demo ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ jacoco-demo ---
[INFO] Surefire report directory: F:\JavaDeveloper\workspaces\SpringMVCDubboExample\jacoco-demo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.xxx.tutorial.Calculator_Test
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.134 sec

Results :

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

[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:report (post-unit-test) @ jacoco-demo ---
[INFO] Analyzed bundle ‘jacoco-demo‘ with 1 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.972 s
[INFO] Finished at: 2017-06-16T21:02:30+08:00
[INFO] Final Memory: 15M/244M
[INFO] ------------------------------------------------------------------------


我們可以看到target目錄下,已經生成了Jacoco的單元測試結果報告~


技術分享


查看Jacoco報告


打開瀏覽器,在URL欄輸入


技術分享


點擊"com.xxx.tutorial"鏈接,查看這個com.xxx.tutorial包下的類。


技術分享


再點擊"Calculator"鏈接,展示Calculator類的方法信息~


技術分享


再點擊任何方法的連接,將會出現該類代碼覆蓋的情況:


綠色的表示覆蓋到的如果沒有覆蓋則會用紅色背景表示


技術分享


至此,


在Maven工程中配置Jacoco插件,運行並查看執行報告結果的示例就完成了~


另外,如果Eclipse工程中安裝了EclEmma插件,執行測試類,


技術分享


也能得到相應的結果,如:


技術分享

更多資料共享請加入Java高並發高可用架構:632103578

Maven工程配置代碼覆蓋工具Jacoco