cobertura-maven-plugin是一个校验单元测试用例覆盖率的工具,可以生成一个测试覆盖率报告,可以给单元测试用例编写提供参考.
  helloword
  cobertura-maven-plugin的使用也很简单,首先你要有源码,然后要有对这个源码编写的测试代码,后在pom.xml中配置上cobertura-maven-plugin执行一行命令可以了.
  我们先来准备一个源码和测试用例:

  要被测试的代码
  packagecom.qyf404.learn.maven;
  publicclassApp{
  publicintadd(inta,intb){
  returna+b;
  }
  publicintsubtract(inta,intb){
  returna-b;
  }
  }
  测试用例代码
  packagecom.qyf404.learn.maven;
  importorg.junit.After;
  importorg.junit.Assert;
  importorg.junit.Before;
  importorg.junit.Test;
  importorg.junit.experimental.categories.Category;
  publicclassAppTest{
  privateAppapp;
  @Before
  publicvoidsetUp(){
  app=newApp();
  }
  @Test
  publicvoidtestAdd()throwsInterruptedException{
  inta=1;
  intb=2;
  intresult=app.add(a,b);
  Assert.assertEquals(a+b,result);
  }
  @Test()
  publicvoidtestSubtract()throwsInterruptedException{
  inta=1;
  intb=2;
  intresult=app.subtract(a,b);
  Assert.assertEquals(a-b,result);
  }
  @After
  publicvoidtearDown()throwsException{
  }
  }
  pom.xml配置如下:
  <?xmlversion="1.0"encoding="UTF-8"?>
  <projectxmlns="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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.qyf404</groupId>
  <artifactId>learn-maven</artifactId>
  <version>1.0-SNAPSHOT</version>
  <build>
  <plugins>
  <plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
  <version>2.7</version>
  </plugin>
  </plugins>
  </build>
  <dependencies>
  <dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
  </dependency>
  </dependencies>
  </project>
  数据都准备好了,我们执行以下maven命令mvncobertura:cobertura,执行完后会在target目录里找到site目录,用浏览器打开里面的index.html,这是测试用例执行完后cobertura-maven-plugin得出的覆盖率报告.