本篇博文咱們將給出示例理解如何在Maven工程中配置Jacoco和如何使用Jacoco查看代碼覆蓋報告~java
Jacoco是一個開源的Java代碼覆蓋率工具,Jacoco能夠嵌入到Ant 、Maven中,並提供了EclEmma Eclipse插件,也能夠使用JavaAgent技術監控Java程序。不少第三方的工具提供了對Jacoco的集成,如sonar、Jenkins等。apache
Maven工程架構
建立Maven工程併發
打開Eclipse,File->New->Project->Maven Project,新建一個Maven工程~maven
點擊「Next」按鈕,而後填寫groupId和artifactId信息後點擊"Finish"按鈕便可~高併發
groupId --> com.xxx.tutorial工具
artifactId --> jacoco-demopost
配置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] ------------------------------------------------------------------------
咱們點擊 Java高併發高可用架構:632103578 就能夠看到target目錄下,已經生成了Jacoco的單元測試結果報告~