建立一个最简单的项目,实践cobertura在maven中应用

cobertura是测试java代码中代码覆盖率的自动化工具

我以前未接触过这个东西,以为直接测试就可以了,但经过试验和阅读文档,明白了,必须经过测试,也就是必须配合xUnit测试才能产生出覆盖率结果,否则结果都是零。下面讲解我做的例子:

目录结构:

 src

 ------main

 -------------HelloWorld.java 

 -------------Calculator.java

-------test

--------------CalculatorTest.java

-------pom.xml

//CalculatorTest.java为jUnit框架Calculator.java对应的测试类,HelloWorld.java没有测试类,最后生成结果会发现HelloWorld的覆盖率为0

HelloWorld.java:

 

  1. public class HelloWorld  
  2. {  
  3. public static void main(String args[ ])  
  4. {  
  5. System.out.println("HelloWorld!");  
  6. }  
  7. }  

Calculator.java

 

 

  1. package andycpp;  
  2. public class Calculator{  
  3.     private static int result; // 静态变量,用于存储运行结果  
  4.     public void add(int n){  
  5.     result = result + n;  
  6.     }  
  7.     public void substract(int n){  
  8.     result = result - 1//Bug: 正确的应该是 result =result-n  
  9.     }  
  10.     public void multiply(int n){  
  11.     } // 此方法尚未写好  
  12.     public void divide(int n){  
  13.     result = result / n;  
  14.     }  
  15.     public void square(int n){  
  16.     result = n * n;  
  17.     }  
  18.   
  19.     public void squareRoot(int n) {  
  20.     for (; ;) ; //Bug : 死循环  
  21.     }  
  22.   
  23.     public void clear() { // 将结果清零  
  24.     result = 0;  
  25.     }  
  26.     public int getResult(){  
  27.     return result;  
  28.     }  
  29. }  


CalculatorTest.java

 

 

  1. import static org.junit.Assert.*;  
  2.     import org.junit.Before;  
  3.     import org.junit.Ignore;  
  4.     import org.junit.Test;  
  5. public class CalculatorTest {  
  6.     private static Calculator calculator = new Calculator();  
  7.     @Before  
  8.     public void setUp() throws Exception {  
  9.     calculator.clear();  
  10.     }  
  11.     @Test  
  12.     public void testAdd() {  
  13.     calculator.add(2);  
  14.     calculator.add(3);  
  15.     assertEquals(5, calculator.getResult());  
  16.     }  
  17.     @Test  
  18.     public void testSubstract() {  
  19.     calculator.add(10);  
  20.     calculator.substract(2);  
  21.     assertEquals(8, calculator.getResult());  
  22.     }  
  23.     @Ignore("Multiply() Not yet implemented")  
  24.     @Test  
  25.     public void testMultiply() {  
  26.     }  
  27.     @Test  
  28.     public void testDivide() {  
  29.     calculator.add(8);  
  30.     calculator.divide(2);  
  31.     assertEquals(4, calculator.getResult());  
  32.     }  
  33. }  

pom.xml:

 

  1. <pre name="code" class="plain"><?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.     
  5.   <groupId>com.xxxcom.helloworld</groupId>  
  6.   <artifactId>helloworld</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>jar</packaging>  
  9.   <url>${HUDSON_URL}</url>  
  10.   
  11.     <build>  
  12.         <sourceDirectory>src/main</sourceDirectory>    
  13.         <testSourceDirectory>src/test</testSourceDirectory>    
  14.         <outputDirectory>target/classes</outputDirectory>    
  15.         <testOutputDirectory>target/test-classes</testOutputDirectory>  
  16.         <plugins>  
  17.             <plugin>  
  18.                 <groupId>org.apache.maven.plugins</groupId>  
  19.                 <artifactId>maven-surefire-plugin</artifactId>  
  20.                 <version>2.12</version>  
  21.             </plugin>  
  22.             <plugin>  
  23.                 <groupId>org.codehaus.mojo</groupId>  
  24.                 <artifactId>cobertura-maven-plugin</artifactId>  
  25.                 <version>2.4</version>  
  26.                 <configuration>  
  27.                     <formats>  
  28.                         <format>html</format>  
  29.                         <format>xml</format>  
  30.                     </formats>  
  31.                     <check>  
  32.                         <branchRate>85</branchRate>  
  33.                         <lineRate>85</lineRate>  
  34.                         <haltOnFailure>true</haltOnFailure>  
  35.                         <totalBranchRate>85</totalBranchRate>  
  36.                         <totalLineRate>85</totalLineRate>  
  37.                         <packageLineRate>85</packageLineRate>  
  38.                         <packageBranchRate>85</packageBranchRate>  
  39.                     </check>  
  40.                 </configuration>  
  41.             </plugin>  
  42.         </plugins>  
  43.     </build>  
  44.     <dependencies>  
  45.         <dependency>  
  46.             <groupId>junit</groupId>  
  47.             <artifactId>junit-dep</artifactId>  
  48.             <version>4.10</version>  
  49.             <scope>test</scope>  
  50.         </dependency>  
  51.     </dependencies>  
  52.       
  53.   <reporting>  
  54.     <plugins>  
  55.       <plugin>  
  56.         <groupId>org.codehaus.mojo</groupId>  
  57.         <artifactId>cobertura-maven-plugin</artifactId>  
  58.         <version>2.4</version>  
  59.         <configuration>  
  60.           <formats>  
  61.             <format>xml</format>  
  62.             <format>html</format>  
  63.           </formats>  
  64.         </configuration>  
  65.       </plugin>  
  66.     </plugins>  
  67.   </reporting>  
  68.     
  69. </project>  




说明:

1.


 

[plain] view plaincopy
  1. <sourceDirectory>src/main</sourceDirectory>    
  2.    <testSourceDirectory>src/test</testSourceDirectory>    
  3.    <outputDirectory>target/classes</outputDirectory>    
  4.    <testOutputDirectory>target/test-classes</testOutputDirectory>  
[plain] view plaincopy
  1. 这些定义环境  
[plain] view plaincopy
  1. <pre name="code" class="plain">2. maven-surefire-plugin执行Junit测试  

3. cobertura-maven-plugin的check标签网上抄的,没明白什么意思,目前

 


[plain] view plaincopy
  1. 4. <dependencies>中定义了junit  
[plain] view plaincopy
  1. 5. <reporting>中指明生成报告的工具cobertura  
posted @ 2012-04-12 13:47  yazhouren  阅读(478)  评论(0)    收藏  举报