Jacoco覆盖率工具使用之maven篇

本文详细介绍如何在Maven项目中集成Jacoco进行代码覆盖率测试。包括创建Maven项目、编写测试类、配置pom.xml文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Jacoco覆盖率工具使用之maven篇

 

说明

之前的文章已经介绍过如何使用apacheant 执行jacoco工具,下面开始介绍如何使用maven使用jacoco工具。

 

1.首先新建一个maven项目

      如图所示:
      

 

2:HelloWorld

    新建一个测试类helloworld,code 如图所示:

  

   

3:HelloWorldTest

  新建一个测试类helloworld test,code 如图所示:


4:编辑pom.xml文件

            编辑pom.xml文件,增加依赖包和jacoco配置,文件如下所示:
        
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <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">  
  2.   <modelVersion>4.0.0</modelVersion>  
  3.   <groupId>com.test.jacoco</groupId>  
  4.   <artifactId>testJacoco</artifactId>  
  5.   <version>0.0.1-SNAPSHOT</version>  
  6.     
  7.   <name>JaCoCo Examples</name>  
  8.   
  9.   <properties>  
  10.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  11.     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>  
  12.   
  13.     <!-- Sonar -->  
  14.     <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>  
  15.     <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>  
  16.     <!-- The destination file for the code coverage report has to be set to the same value  
  17.          in the parent pom and in each module pom. Then JaCoCo will add up information in  
  18.          the same report, so that, it will give the cross-module code coverage. -->  
  19.     <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>  
  20.     <sonar.language>java</sonar.language>  
  21.   </properties>  
  22.   
  23.   <dependencies>  
  24.   <!--   
  25.     <dependency>  
  26.       <groupId>junit</groupId>  
  27.       <artifactId>junit</artifactId>  
  28.       <version>4.8.1</version>  
  29.       <scope>test</scope>  
  30.     </dependency>  
  31.      -->  
  32.     <dependency>  
  33.   <groupId>junit</groupId>  
  34.   <artifactId>junit</artifactId>  
  35.   <version>4.11</version>  
  36.   <scope>test</scope>  
  37. </dependency>  
  38.   </dependencies>  
  39.   
  40.   <build>  
  41.     <pluginManagement>  
  42.       <plugins>  
  43.         <plugin>  
  44.           <groupId>org.jacoco</groupId>  
  45.           <artifactId>jacoco-maven-plugin</artifactId>  
  46.           <version>0.5.3.201107060350</version>  
  47.         </plugin>  
  48.       </plugins>  
  49.     </pluginManagement>  
  50.   
  51.     <plugins>  
  52.       <plugin>  
  53.         <groupId>org.jacoco</groupId>  
  54.         <artifactId>jacoco-maven-plugin</artifactId>  
  55.         <configuration>  
  56.           <includes>com.*</includes>  
  57.         </configuration>  
  58.         <executions>  
  59.           <execution>  
  60.             <id>pre-test</id>  
  61.             <goals>  
  62.               <goal>prepare-agent</goal>  
  63.             </goals>  
  64.           </execution>  
  65.           <execution>  
  66.             <id>post-test</id>  
  67.             <phase>test</phase>  
  68.             <goals>  
  69.               <goal>report</goal>  
  70.             </goals>  
  71.           </execution>  
  72.         </executions>  
  73.       </plugin>  
  74.       <plugin>  
  75.         <groupId>org.apache.maven.plugins</groupId>  
  76.         <artifactId>maven-compiler-plugin</artifactId>  
  77.         <configuration>  
  78.           <source>1.5</source>  
  79.           <target>1.5</target>  
  80.         </configuration>  
  81.        </plugin>  
  82.     </plugins>  
  83.   </build>  
  84.    
  85. </project>  

5:打包测试

    如图所示:

    

6: 执行结果

   执行结果:




至此,基于maven的jacoco的使用讲解完了,整合jenkins 和 sonar 请参考“Jacoco覆盖率工具使用”;
转载:http://blog.youkuaiyun.com/wangmuming/article/details/28868833
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值