使用Java程序生成Cobertura.ser文件

时间:2015-03-25 07:04:52

标签: cobertura

是否可以使用Java程序生成“cobertura.ser”文件?

我可以看到cobertura.ser是通过cobertura maven插件生成的,我能够读取此文件,如下所示,

示例服务类

public class SampleService {

public String sampleMethod(int a) {
    if(a==3) {
        return "equal";
    }
    else {
        return "notequal";
    }
}
}

样本测试类

import org.junit.Ignore;
import org.junit.Test;
import org.junit.Assert; 

public class SampleServiceTest {

@Ignore
@Test
public void testSampleMethod1(){
    SampleService sampleService = new SampleService();
    String val = sampleService.sampleMethod(3);
    Assert.assertEquals("equal", val);
}

//@Ignore
@Test
public void testSampleMethod2(){
    SampleService sampleService = new SampleService();
    String val = sampleService.sampleMethod(5);
    Assert.assertEquals("notequal", val);
}

}

cobertura.ser读者类

import java.io.File;
import net.sourceforge.cobertura.coveragedata.ClassData;
import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
import net.sourceforge.cobertura.coveragedata.ProjectData;

public class SampleCoberturaCoverage {

  public static void main(String[] args) {

    File dataFile = new File("C:\\Users\\Naren\\workspace\\cobsample\\target\\cobertura\\cobertura.ser");
    ProjectData projectData = CoverageDataFileHandler.loadCoverageData(dataFile);
    ClassData classData = projectData.getClassData("SampleService");
    System.out.println(classData.getNumberOfValidBranches());
  }
}

我需要使用Java程序生成文件,而不是使用Maven插件运行。

0 个答案:

没有答案