Introduction to Cobertura – 科贝尔图拉简介

最后修改: 2017年 2月 25日


1. Overview


In this article, we will demonstrate several aspects of generating code coverage reports using Cobertura.


Simply put, Cobertura is a reporting tool that calculates test coverage for a codebase – the percentage of branches/lines accessed by unit tests in a Java project.


2. Maven Plugin

2.Maven Plugin

2.1. Maven Configuration


In order to start calculating code coverage in your Java project, you need to declare the Cobertura Maven plugin in your pom.xml file under the reporting section:

为了在您的Java项目中开始计算代码覆盖率,您需要在pom.xml文件中的报告部分声明Cobertura Maven插件。


You can always check the latest version of the plugin in the Maven central repository.


Once done, go ahead and run Maven specifying cobertura:cobertura as a goal.


This will create a detailed HTML style report showing code coverage statistics gathered via code instrumentation:



The line coverage metric shows how many statements are executed in the Unit Tests run, while the branch coverage metric focuses on how many branches are covered by those tests.


For each conditional, you have two branches, so basically, you’ll end up having twice as many branches as conditionals.


The complexity factor reflects the complexity of the code — it goes up when the number of branches in code increases.


In theory, the more branches you have, the more tests you need to implement in order to increase the branch coverage score.


2.2. Configuring Code Coverage Calculation and Checks


You can ignore/exclude a specific set of classes from code instrumentation using the ignore and the exclude tags:



After calculating the code coverage comes the check phase. The check phase ensures that a certain level of code coverage is reached.


Here’s a basic example on how to configure the check phase:



When using the haltOnFailure flag, Cobertura will cause the build to fail if one of the specified checks fail.


The branchRate/lineRate tags specify the minimum acceptable branch/line coverage score required after code instrumentation. These checks can be expanded to the package level using the packageLineRate/packageBranchRate tags.


It is also possible to declare specific rule checks for classes with names following a specific pattern by using the regex tag. In the example above, we ensure that a specific line/branch coverage score must be reached for classes in the com.baeldung.algorithms.dijkstra package and below.


3. Eclipse Plugin


3.1. Installation


Cobertura is also available as an Eclipse plugin called eCobertura. In order to install eCobertura for Eclipse, you need to follow the steps below and have Eclipse version 3.5 or greater installed:


Step 1: From the Eclipse menu, select HelpInstall New Software. Then, at the work with the field, enter

第1步:从Eclipse菜单中,选择帮助安装新软件。然后,在 work with字段,输入


Step 2: Select eCobertura Code Coverage, click “next”, and then follow the steps in the installation wizard.

第2步:选择eCobertura代码覆盖率,点击 “下一步”,然后按照安装向导的步骤操作。

Now that eCobertura is installed, restart Eclipse and show the coverage session view under Windows → Show View → Other → Cobertura.



3.2. Using Eclipse Kepler or Later

3.2.使用Eclipse Kepler或更高版本

For the newer version of Eclipse (Kepler, Luna, etc.), the installation of eCobertura may cause some problems related to JUnit — the newer version of JUnit packaged with Eclipse is not fully compatible with eCobertura‘s dependencies checker:


Cannot complete the install because one or more required items could not be found.
  Software being installed: eCobertura (
  Missing requirement: eCobertura UI (ecobertura.ui requires 'bundle org.junit4 0.0.0' but it could not be found
  Cannot satisfy dependency:
    From: eCobertura 
    To: ecobertura.ui []

As a workaround, you can download an older version JUnit and place it into the Eclipse plugins folder.


This can be done by deleting the folder org.junit.*** from %ECLIPSE_HOME%/plugins, and then copying the same folder from an older Eclipse installation that is compatible with eCobertura.


Once done, restart your Eclipse IDE and re-install the plugin using the corresponding update site.

完成后,重新启动你的Eclipse IDE,并使用相应的更新站点重新安装该插件

3.3. Code Coverage Reports in Eclipse


In order to calculate code coverage by a Unit Test, right-click your project/test to open the context menu, then choose the option Cover As → JUnit Test.

为了计算单元测试的代码覆盖率,右击你的项目/测试,打开上下文菜单,然后选择Cover As → JUnit Test选项。

Under the Coverage Session view, you can check the line/branch coverage report per class:



Java 8 users may encounter a common error when calculating code coverage:

Java 8用户在计算代码覆盖率时可能会遇到一个常见错误:

java.lang.VerifyError: Expecting a stackmap frame at branch target ...

In this case, Java is complaining about some methods not having a proper stack map, due to the stricter bytecode verifier introduced in newer versions of Java.


This issue can be solved by disabling verification in the Java Virtual Machine.


To do so, right-click your project to open the context menu, select Cover As, and then open the Coverage Configurations view. In the arguments tab, add the -noverify flag as a VM argument. Finally, click on the coverage button to launch coverage calculation.

要做到这一点,右键单击您的项目以打开上下文菜单,选择Cover As,然后打开Coverage Configurations视图。在参数选项卡中,添加-noverify标志作为VM参数。最后,点击coverage按钮,启动覆盖率计算。

You can also use the flag -XX:-UseSplitVerifier, but this only works with Java 6 and 7, as the split verifier is no longer supported in Java 8.

你也可以使用标志-XX:-UseSplitVerifier,但这只适用于Java 6和7,因为Java 8中不再支持分割验证器。

4. Conclusion


In this article, we have shown briefly how to use Cobertura to calculate code coverage in a Java project. We have also described the steps required to install eCobertura in your Eclipse environment.


Cobertura is a great yet simple code coverage tool, but not actively maintained, as it is currently outclassed by newer and more powerful tools like JaCoCo.


Finally, you can check out the example provided in this article in the GitHub project.