Jacoco Coverage Tool use MAVEN article _ Unit Test

Source: Internet
Author: User

Description

Previous articles have described how to use Apacheant to execute the Jacoco tool, and here's how to use MAVEN to use the Jacoco tool.

1. First create a new MAVEN project as shown in the figure:

2:helloworld

Create a new test class Helloworld,code as shown in the figure:

3:helloworldtest

Create a new test class HelloWorld Test,code as shown in the figure:


4: Edit Pom.xml file             Edit Pom.xml file, add Dependency Pack and Jacoco configuration, file as follows:       &NBSP;&NB Sp [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: Packaging test

As shown in the figure:

6: Implementation results of the execution results:



At this point, based on the use of Maven Jacoco, integrated Jenkins and Sonar please refer to the "Jacoco coverage tool use";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.