Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage. It is based on jcoverage. Click Me
Requirement:
Operating System, Java, Ant, Eclipse and Cobetura.
Note; Download latest cobetura files from http://cobertura.github.io/cobertura/
Create the new java project in eclipse:
Open the eclipse.
File > New > Java Project
Enter the name: cobertura_java_project
Right Click on the src under project cobertura_java_project
New > Class
Enter the:
package name: tutorialbyexample.cobertura.com
Class Name: CoberturaJavaApp.java
CoberturaJavaApp.java file look like as below:
/**
*
*/
package tutorialbyexample.cobertura.com;
/**
* @author Vinod Kumar
*
*/
public class CoberturaJavaApp {
/**
* @param args
*/
public static void main(String[] args) {
CoberturaJavaApp obj = new CoberturaJavaApp();
printData("Welcome " + obj.getName("Vinod")
+ " to learning Cobertura!!!");
}
public String getName(String name) {
return name;
}
public String getAnotherName(String name) {
return name;
}
private static void printData(Object obj) {
System.out.println(obj);
}
}
Output: Welcome Vinod to learning Cobertura!!!
Create the another simple project in eclipse: Cobetura-Java-Coverage-Report
Add the:
lib folder under Cobetura-Java-Coverage-Report
asm-5.0.1.jar
asm-analysis-5.0.1.jar
asm-commons-5.0.1.jar
asm-tree-5.0.1.jar
asm-util-5.0.1.jar
cobertura-2.1.1-javadoc.jar
cobertura-2.1.1-sources.jar
cobertura-2.1.1.jar
commons-lang3-3.3.2.jar
hamcrest-core-1.3.jar
jaxen-1.1.4.jar
jetty-6.1.14.jar
jetty-util-6.1.14.jar
junit-4.11.jar
logback-classic-1.0.13.jar
logback-core-1.0.13.jar
oro-2.0.8.jar
servlet-api-2.5-6.1.14.jar
slf4j-api-1.7.5.jar
build.xml
cobertura.properties
cobertura.properties:
lib.dir=C:/Users/workspace/Cobetura-Java-Coverage-Report/lib
instrumented.dir=C:/Users/workspace/Cobetura-Java-Coverage-Report/instrumented
coverage.html.dir=C:/Users/workspace/Cobetura-Java-Coverage-Report/htmlReports
src.module1.dir=C:/Users/workspace/cobertura_java_project/src
target.module1.dir=C:/Users/workspace/cobertura_java_project/bin
build.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="Cobetura-Java-Coverage-Report" default="GeneratingCoberturaFiles" basedir=".">
<property file="cobertura.properties" />
<property name="lib.dir" value="${lib.dir}" />
<property name="instrumented.dir" value="${instrumented.dir}" />
<property name="coverage.html.dir" value="${coverage.html.dir}" />
<property name="src.module1.dir" value="${src.module1.dir}" />
<property name="target.module1.dir" value="${target.module1.dir}" />
<path id="cobertura.classpath">
<fileset dir="${lib.dir}">
<include name="*.jar" />
</fileset>
</path>
<taskdef classpathref="cobertura.classpath" resource="tasks.properties" />
<target name="init">
<delete dir="${instrumented.dir}" />
<delete dir="${coverage.html.dir}" />
<delete file="cobertura.ser" />
<mkdir dir="${instrumented.dir}" />
<mkdir dir="${coverage.html.dir}" />
</target>
<target name="GeneratingCoberturaFiles" depends="init" description="Generating the .ser file with .class file">
<cobertura-instrument todir="${target.module1.dir}">
<ignore regex="org.apache.log4j.*" />
<fileset dir="${target.module1.dir}">
<include name="**/*.class" />
</fileset>
</cobertura-instrument>
</target>
<target name="GenerateHTMLReport" description="Generate coverage report">
<delete dir="${coverage.html.dir}" />
<mkdir dir="${coverage.html.dir}" />
<cobertura-report destdir="${coverage.html.dir}">
<fileset dir="${src.module1.dir}">
<include name="**/*.java" />
</fileset>
</cobertura-report>
</target>
</project>
Open the build.xml in ant view windows.
Click on the GeneratingCoberturaFiles task
Buildfile: C:\Users\workspace\Cobetura-Java-Coverage-Report\build.xml
init:
[delete] Deleting directory C:\Users\workspace\Cobetura-Java-Coverage-Report\instrumented
[delete] Deleting directory C:\Users\workspace\Cobetura-Java-Coverage-Report\htmlReports
[delete] Deleting: C:\Users\workspace\Cobetura-Java-Coverage-Report\cobertura.ser
[mkdir] Created dir: C:\Users\workspace\Cobetura-Java-Coverage-Report\instrumented
[mkdir] Created dir: C:\Users\workspace\Cobetura-Java-Coverage-Report\htmlReports
GeneratingCoberturaFiles:
[cobertura-instrument] 16:05:22,347 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-instrument] 16:05:22,347 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-instrument] 16:05:22,347 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 16:05:22,348 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-instrument] 16:05:22,348 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 16:05:22,348 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-instrument] 16:05:22,371 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@c3e952 - URL [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-instrument] 16:05:22,404 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-instrument] 16:05:22,411 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-instrument] 16:05:22,423 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-instrument] 16:05:22,438 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-instrument] 16:05:22,479 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-instrument] 16:05:22,479 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-instrument] 16:05:22,479 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-instrument] 16:05:22,480 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-instrument] 16:05:22,481 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@b1959 - Registering current configuration as safe fallback point
[cobertura-instrument] 16:05:22,850 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-instrument] 16:05:22,850 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-instrument] 16:05:22,850 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 16:05:22,850 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-instrument] 16:05:22,851 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-instrument] 16:05:22,851 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-instrument] 16:05:22,865 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@193b604 - URL [jar:file:/C:/Users/extvkx/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-instrument] 16:05:22,909 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-instrument] 16:05:22,917 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-instrument] 16:05:22,923 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-instrument] 16:05:22,938 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-instrument] 16:05:22,965 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-instrument] 16:05:22,965 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-instrument] 16:05:22,965 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-instrument] 16:05:22,965 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-instrument] 16:05:22,966 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@92e96c - Registering current configuration as safe fallback point
[cobertura-instrument] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-instrument] [INFO] Cobertura: Saved information on 1 classes.
[cobertura-instrument] [INFO] Cobertura: Saved information on 1 classes.
BUILD SUCCESSFUL
Total time: 1 second
Now go to the cobertura_java_project project:
Right click on the CoberturaJavaApp.java then Run as > run Configuration
Enter the main class: tutorialbyexample.cobertura.com.CoberturaJavaApp
Click on the Argument tab: -Dnet.sourceforge.cobertura.datafile=C:/Users/workspace/Cobetura-Java-Coverage-Report/cobertura.ser
In calss path add the:
asm-5.0.1.jar
asm-analysis-5.0.1.jar
asm-commons-5.0.1.jar
asm-tree-5.0.1.jar
asm-util-5.0.1.jar
cobertura-2.1.1-javadoc.jar
cobertura-2.1.1-sources.jar
cobertura-2.1.1.jar
commons-lang3-3.3.2.jar
hamcrest-core-1.3.jar
jaxen-1.1.4.jar
jetty-6.1.14.jar
jetty-util-6.1.14.jar
junit-4.11.jar
logback-classic-1.0.13.jar
logback-core-1.0.13.jar
oro-2.0.8.jar
servlet-api-2.5-6.1.14.jar
slf4j-api-1.7.5.jar
Click on the apply
Click on the run
Output:
16:11:21,236 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
16:11:21,236 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
16:11:21,237 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
16:11:21,237 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
16:11:21,237 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
16:11:21,237 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1.jar!/logback.xml]
16:11:21,251 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@8bf135 - URL [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
16:11:21,293 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
16:11:21,301 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
16:11:21,308 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
16:11:21,324 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
16:11:21,352 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
16:11:21,352 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
16:11:21,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
16:11:21,353 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
16:11:21,354 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1890c67 - Registering current configuration as safe fallback point
Welcome Vinod to learning Cobertura!!!
[INFO] Cobertura: Loaded information on 1 classes.
[INFO] Cobertura: Saved information on 1 classes.
Go the the ant view and click on the GenerateHTMLReport
Buildfile: C:\Users\workspace\Cobetura-Java-Coverage-Report\build.xml
GenerateHTMLReport:
[delete] Deleting directory C:\Users\workspace\Cobetura-Java-Coverage-Report\htmlReports
[mkdir] Created dir: C:\Users\workspace\Cobetura-Java-Coverage-Report\htmlReports
[cobertura-report] 16:12:51,916 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-report] 16:12:51,916 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-report] 16:12:51,917 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 16:12:51,917 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-report] 16:12:51,917 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 16:12:51,917 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-report] 16:12:51,947 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@3ec403 - URL [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-report] 16:12:51,980 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-report] 16:12:51,986 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-report] 16:12:51,998 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-report] 16:12:52,013 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-report] 16:12:52,053 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-report] 16:12:52,054 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-report] 16:12:52,054 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-report] 16:12:52,054 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-report] 16:12:52,056 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@b6dfa8 - Registering current configuration as safe fallback point
[cobertura-report] 16:12:52,225 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
[cobertura-report] 16:12:52,225 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
[cobertura-report] 16:12:52,225 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 16:12:52,225 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
[cobertura-report] 16:12:52,225 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml]
[cobertura-report] 16:12:52,225 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1.jar!/logback.xml]
[cobertura-report] 16:12:52,239 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@8bf135 - URL [jar:file:/C:/Users/workspace/Cobetura-Java-Coverage-Report/lib/cobertura-2.1.1-sources.jar!/logback.xml] is not of type file
[cobertura-report] 16:12:52,281 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
[cobertura-report] 16:12:52,289 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
[cobertura-report] 16:12:52,295 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
[cobertura-report] 16:12:52,309 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
[cobertura-report] 16:12:52,336 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [net.sourceforge.cobertura] to INFO
[cobertura-report] 16:12:52,336 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
[cobertura-report] 16:12:52,336 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
[cobertura-report] 16:12:52,336 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
[cobertura-report] 16:12:52,337 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@1890c67 - Registering current configuration as safe fallback point
[cobertura-report] Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
[cobertura-report] [INFO] Cobertura: Loaded information on 1 classes.
[cobertura-report] Report time: 80ms
BUILD SUCCESSFUL
Total time: 1 second
Report has been generated:
Right click on the htmlReports under Cobetura-Java-Coverage-Report
Properties then copy the location: C:\Users\workspace\Cobetura-Java-Coverage-Report\htmlReports
Double click on the index.html
Have good day ahead!!!
Thank you so muchh! The step by step procedure helped a lot!
ReplyDeleteThanks for like and it solved your purpose with http://www.tutorialbyexample.com/2015/03/cobertura-java-ant-eclipse-example.html.
DeleteHi everybody,
DeleteI followed the same steps and I am able to generate the reports but not able to see the code coverage.. below is output when I click on line coverage and branch coverage..
Line coverage:the percentage of lines executed by thid test run
Branch coverage:the percentage of lines executed by thid test run
N/A: line coverage and branch coverage will appear as not applicable when cobertura can't find the line number information in the . class file.this happens for stub and skeleton classes, interfaces or when the class was not compiled with debug = true.
Hi everybody,
DeleteI followed the same steps and I am able to generate the reports but not able to see the code coverage.. below is output when I click on line coverage and branch coverage..
Line coverage:the percentage of lines executed by thid test run
Branch coverage:the percentage of lines executed by thid test run
N/A: line coverage and branch coverage will appear as not applicable when cobertura can't find the line number information in the . class file.this happens for stub and skeleton classes, interfaces or when the class was not compiled with debug = true.
Thanks for your info about Cobertura in Java by Ant in eclipse.
DeleteCan you double check for .ser file and you are running application like >
Run It: Right click on the CoberturaJavaApp.java then Run as > Java Application
And most of time people use to miss for path and all run the simple program from http://cobertura.github.io/cobertura/
Output: Welcome Vinod to learning Cobertura!!!
Can you please share the code
ReplyDelete