Here at Siniatech we’ve been looking at kicking off a new project that is mostly going to be dealing with a whole load of file management. While we’ve been involved in a lot of Scala development recently, we thought we’d give Java 7 a bit of a spin as its NIO 2.0 features seemed to provide a lot of new functionality that was just going to make our life a whole lot easier (let’s face it, scala.io is not the best bit of Scala).
Additionally, we thought we’d use this opportunity to consider using a code coverage tool from the ground-up. I’ve always been in two-minds about such tools and the value they provide. I can see that they can ensure that you’ve tested everything, but not necessarily that you’ve tested it well. I’ve generally felt that a good developer would write good tests that covered the important stuff with or without a coverage tool, but a poor developer will simply write tests to game the system by writing tests to ensure that every line of code is used rather than the system actually behaves how it’s supposed to.
Surely the development of tests is partly there to help a developer spot their misunderstandings, and I’m worried that coverage tools simply encourage you to compound those. When I write a test I do so from a completely functional point of view, i.e. I write a test to cover use cases of the class (and no, I’m not talking about integration tests – just behavioural tests). When I’ve seen people use coverage tools before, I have found that they simply write one or more tests for each method to cover the various branches without really thinking about what the class actually ‘does’.Despite my reservations, we have obviously used coverage tools before, but mostly these have been brought in part-way through a project, so I decided it was worth giving coverage a go on a new project, and using it properly from the start.
Anyway, so once we’d decided to go ahead with Java 7 and code coverage, we got to work cutting some code and created some Maven projects. We plumbed in Cobertura and all seemed to go swimmingly at first, with the plugin able to generate some coverage statistics for some relatively simple classes and tests. However, as the number and size of the tests grew, we started to hit some issues; all the tests would pass during a normal run, but as soon as Cobertura added its instrumentation we were seeing stack frame errors everywhere. We pushed on trying various configurations assuming we were having some finger-trouble, but before long we turned to Google and discovered that Cobertura wasn’t actually compatible with Java 7. This seemed a bit odd to us given that Java 7 has been out for a while now, but a bit more research seemed to show that actually there were no code coverage tools available for Java 7 unless we were prepared to pay for Atlassian’s Clover. A bit perturbed with the whole thing we ended up giving up on it for a while, especially as we were busy with other projects.
Obviously, I was a bit dumbfounded that their could be no way to get code coverage working with Java 7, so I continued Googling when I got the change and had a closer look at the various coverage tools available. Eventually I came across JaCoCo. At first I didn’t hold out much hope, especially as it’s part of the EclEmma suite which I’d already tried without success. However, I observed that the latest release was from January 2012 and there did appear to be some hints that it was compatible on the fairly sparse website. That website didn’t contain a great detail of information on how to plumb into Maven either, but I managed to cobble together something that appeared to work. However, whenever I tried to generate the report, an IOException was thrown complaining about a missing file!
Back to Google once more, and having trawled numerous websites (mostly aimed at getting JaCoCo working under Sonar, which wasn’t what I wanted), I was able to piece together the following XML. I now have a working coverage report, but given the length of time it took I to get there I thought it was worth posting so that it might help others in need. I’ll report back later on whether we find using this tool with our new project to be a help or hindrance.
Note that, Eclipse reports an error on line 10, but despite this the pom is able to install and generate the reports successfully – even under Eclipse. If I get a chance I’ll investigate and update.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| <build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.6.201201232323</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>coverageAgent</propertyName>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xmx256m ${coverageAgent}</argLine>
</configuration>
</plugin>
</plugins>
</build> |