I’ve recently reached out for Cobertura to add test coverage reporting to one of my project. One of the first challenges I’ve face was integrating the plugin into a project powered by Gradle. Turns out there is a Gradle version of the plugin available (credit: Steve Saliman).

https://gist.github.com/zezutom/8526e229a4ab01d7a69f

The problem I was unable to work around when using Cobertura was the lack of support for all features of Java 8. For instance, Cobertura won’t recognize Lambda expressions Let me use an example from my previous post.

The Lambda expression at line 6 in the code excerpt below …

public List<Count> count(String text) {
 return text
          .toLowerCase()
          .chars()
          .distinct()
          .mapToObj(i -> new Count((char) i, 
             text.toLowerCase()
             .chars()
             .filter(j -> j == i)
             .count()))
             .sorted()
          .collect(Collectors.toList());
}
..

… will result into a failure when running Cobertura, see below.

Cobertura 2.1.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT file
net.sourceforge.cobertura.javancss.parser.ParseException: Encountered " ">" "> "" at line 6, column 46.
Was expecting one of:
 "assert" ...
 "boolean" ...
 "byte" ...
 "char" ...
 "double" ...
 "enum" ...
 "false" ...
 "float" ...
 "int" ...
 "long" ...
 "new" ...
 "null" ...
 "short" ...
 "super" ...
 "this" ...
 "true" ...
 "void" ...
 <INTEGER_LITERAL> ...
 <FLOATING_POINT_LITERAL> ...
 <CHARACTER_LITERAL> ...
 <STRING_LITERAL> ...
 <IDENTIFIER> ...
 "(" ...
 "!" ...
 "~" ...
 "++" ...
 "--" ...
 "+" ...
 "-" ...

As far as I can tell, there is no fix available just yet, feel free to follow this discussion to see the latest. Meanwhile, I found out that JaCoCo does about the same job as Cobertura and, more importantly, supports Java 8 out of the box. Adding the JaCoCo plugin into a Gradle build is very easy, see the Gist below.

https://gist.github.com/zezutom/453c6db042e6cacf83d9


Tomas Zezula

Hello! I'm a technology enthusiast with a knack for solving problems and a passion for making complex concepts accessible. My journey spans across software development, project management, and technical writing. I specialise in transforming rough sketches of ideas to fully launched products, all the while breaking down complex processes into understandable language. I believe a well-designed software development process is key to driving business growth. My focus as a leader and technical writer aims to bridge the tech-business divide, ensuring that intricate concepts are available and understandable to all. As a consultant, I'm eager to bring my versatile skills and extensive experience to help businesses navigate their software integration needs. Whether you're seeking bespoke software solutions, well-coordinated product launches, or easily digestible tech content, I'm here to make it happen. Ready to turn your vision into reality? Let's connect and explore the possibilities together.