建立一个最简单的项目,实践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:

public class HelloWorld
{
public static void main(String args[ ])
{
System.out.println("HelloWorld!");
}
}
Calculator.java

package andycpp;
public class Calculator{
    private static int result; // 静态变量,用于存储运行结果
    public void add(int n){
	result = result + n;
	}
	public void substract(int n){
	result = result - 1; //Bug: 正确的应该是 result =result-n
	}
	public void multiply(int n){
	} // 此方法尚未写好
	public void divide(int n){
	result = result / n;
	}
	public void square(int n){
	result = n * n;
	}

	public void squareRoot(int n) {
	for (; ;) ; //Bug : 死循环
	}

	public void clear() { // 将结果清零
	result = 0;
	}
	public int getResult(){
	return result;
	}
}

CalculatorTest.java

import static org.junit.Assert.*;
	import org.junit.Before;
	import org.junit.Ignore;
	import org.junit.Test;
public class CalculatorTest {
	private static Calculator calculator = new Calculator();
	@Before
	public void setUp() throws Exception {
	calculator.clear();
	}
	@Test
	public void testAdd() {
	calculator.add(2);
	calculator.add(3);
	assertEquals(5, calculator.getResult());
	}
	@Test
	public void testSubstract() {
	calculator.add(10);
	calculator.substract(2);
	assertEquals(8, calculator.getResult());
	}
	@Ignore("Multiply() Not yet implemented")
	@Test
	public void testMultiply() {
	}
	@Test
	public void testDivide() {
	calculator.add(8);
	calculator.divide(2);
	assertEquals(4, calculator.getResult());
	}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.xxxcom.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  <url>${HUDSON_URL}</url>

    <build>
	    <sourceDirectory>src/main</sourceDirectory>  
        <testSourceDirectory>src/test</testSourceDirectory>  
        <outputDirectory>target/classes</outputDirectory>  
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<version>2.12</version>
			</plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>cobertura-maven-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <formats>
                        <format>html</format>
                        <format>xml</format>
                    </formats>
					<check>
						<branchRate>85</branchRate>
						<lineRate>85</lineRate>
						<haltOnFailure>true</haltOnFailure>
						<totalBranchRate>85</totalBranchRate>
						<totalLineRate>85</totalLineRate>
						<packageLineRate>85</packageLineRate>
						<packageBranchRate>85</packageBranchRate>
				    </check>
                </configuration>
            </plugin>
        </plugins>
    </build>
	<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit-dep</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
	
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>cobertura-maven-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <formats>
            <format>xml</format>
            <format>html</format>
          </formats>
        </configuration>
      </plugin>
    </plugins>
  </reporting>
  
</project>



说明:

1.


	    <sourceDirectory>src/main</sourceDirectory>  
        <testSourceDirectory>src/test</testSourceDirectory>  
        <outputDirectory>target/classes</outputDirectory>  
        <testOutputDirectory>target/test-classes</testOutputDirectory>
这些定义环境
2. maven-surefire-plugin执行Junit测试
3. cobertura-maven-plugin的check标签网上抄的,没明白什么意思,目前


4. <dependencies>中定义了junit
5. <reporting>中指明生成报告的工具cobertura


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值