java覆盖率之Cobertura生成覆盖率

一. 下载maven

$ jumbo install apache-maven


二. 构建mvn项目

$ mkdir maven_project

$ cd maven_project

$ vim pom.xml; #输入一下内容

                                                                

                                                                                             

     4.0.0                                                                 

     se.thinkcode                                                                    

     one-module-example                                                        

     1.0                                                                             

                                                                                           

         UTF-8                             

         UTF-8                     

                                                                                         

                                                                                                

                                                                                              

                                                                                               

                 org.codehaus.mojo                                                   

                 cobertura-maven-plugin                                        

                 2.5.1                                                               

                                                                                           

                                                                                            

                         process-classes                                                 

                                                                                                

                             cobertura                                                     

                                                                                              

                                                                                          

                                                                                         

                                                                                             

                                                                                            

                                                                                              

                                                                                         

                                                                                           

             junit                                                                   

             junit                                                             

             4.10                                                                    

                                                                                         

                                                                                       


$ mkdir src/main/java/se/sigma/calculator/

$ vim src/main/java/se/sigma/calculator/Calculator.java;#输入一下内容:

package se.sigma.calculator;                                                                          

                                                                                                        

public class Calculator {                                                                             

    public int nextFibonacci(int a, int b) {                                                          

        int sum = a + b;                                                                              

        return sum;                                                                                   

    }                                                                                                 

    public int sum(int a, int b) {                                                                    

        return a + b;                                                                                 

    }                                                                                                 

}

$ mkdir test/java/se/sigma/calculator/

$ vim test/java/se/sigma/calculator/CalculatorTest.java;#输入以下内容:

package se.sigma.calculator;                                                                          

                                                                                                        

import org.junit.Test;                                                                                

                                                                                                        

import static org.hamcrest.core.Is.is;                                                                

import static org.junit.Assert.assertThat;                                                            

                                                                                                        

public class CalculatorTest {                                                                         

                                                                                                        

    @Test                                                                                             

    public void shouldCalculateFibonacci() {                                                          

        Calculator calculator = new Calculator();                                                     

        int expected = 13;                                                                            

        int actual = calculator.nextFibonacci(5, 8);                                                  

                                                                                                        

        assertThat(actual, is(expected));                                                             

    }                                                                                                 

}

 

三. 生成覆盖率

$ mvn cobertura:cobertura


四. 访问覆盖率统计数据

$ cd target/site/cobertura/index.html


五. 在jenkins上配置,类似于下方:



六. 效果展示