Jacoco覆蓋率工具使用之maven篇_單元測試

來源:互聯網
上載者:User

說明

之前的文章已經介紹過如何使用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 <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.test.jacoco</groupId>     <artifactId>testJacoco</artifactId>     <version>0.0.1-SNAPSHOT</version>          <name>JaCoCo Examples</name>        <properties>       <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>       <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>          <!-- Sonar -->       <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>       <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>       <!-- The destination file for the code coverage report has to be set to the same value            in the parent pom and in each module pom. Then JaCoCo will add up information in            the same report, so that, it will give the cross-module code coverage. -->       <sonar.jacoco.itReportPath>${project.basedir}/target/jacoco.exec</sonar.jacoco.itReportPath>       <sonar.language>java</sonar.language>     </properties>        <dependencies>     <!--        <dependency>         <groupId>junit</groupId>         <artifactId>junit</artifactId>         <version>4.8.1</version>         <scope>test</scope>       </dependency>        -->       <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.11</version>     <scope>test</scope>   </dependency>     </dependencies>        <build>       <pluginManagement>         <plugins>           <plugin>             <groupId>org.jacoco</groupId>             <artifactId>jacoco-maven-plugin</artifactId>             <version>0.5.3.201107060350</version>           </plugin>         </plugins>       </pluginManagement>          <plugins>         <plugin>           <groupId>org.jacoco</groupId>           <artifactId>jacoco-maven-plugin</artifactId>           <configuration>             <includes>com.*</includes>           </configuration>           <executions>             <execution>               <id>pre-test</id>               <goals>                 <goal>prepare-agent</goal>               </goals>             </execution>             <execution>               <id>post-test</id>               <phase>test</phase>               <goals>                 <goal>report</goal>               </goals>             </execution>           </executions>         </plugin>         <plugin>           <groupId>org.apache.maven.plugins</groupId>           <artifactId>maven-compiler-plugin</artifactId>           <configuration>             <source>1.5</source>             <target>1.5</target>           </configuration>          </plugin>       </plugins>     </build>       </project>  
5:打包測試

    如圖所示:

     6: 執行結果    執行結果:



至此,基於maven的jacoco的使用講解完了,整合jenkins 和 sonar 請參考“Jacoco覆蓋率工具使用”;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.