X

Geertjan's Blog

  • June 15, 2012

Code Coverage for Maven Integrated in NetBeans IDE 7.2

Geertjan Wielenga
Product Manager

In NetBeans IDE 7.2, JaCoCo is supported natively, i.e., out of the box, as a code coverage engine for Maven projects, since Cobertura does not work with JDK 7 language constructs. (Although, note that Cobertura is supported as well in NetBeans IDE 7.2.) It isn't part of NetBeans IDE 7.2 Beta, so don't even try there; you need some development build from after that. I downloaded the latest development build today.

To enable JaCoCo features in NetBeans IDE, you need do no different to what you'd do when enabling JaCoCo in Maven itself, which is rather wonderful. In both cases, all you need to do is add this to the "plugins" section of your POM:

<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.5.7.201204190339</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

Now you're done and ready to examine the code coverage of your tests, whether they are JUnit or TestNG. At this point, i.e., for no other reason than that you added the above snippet into your POM, you will have a new Code Coverage menu when you right-click on the project node:

If you click Show Report above, the Code Coverage Report window opens. Here, once you've run your tests, you can actually see how many classes have been covered by your tests, which is pretty useful since 100% tests passing doesn't mean much when you've only tested one class, as you can see very graphically below:

Then, when you click the bars in the Code Coverage Report window, the class under test is shown, with the methods for which tests exist highlighted in green and those that haven't been covered in red:

(Note: Of course, striving for 100% code coverage is a bit nonsensical. For example, writing tests for your getters and setters may not be the most useful way to spend one's time. But being able to measure, and visualize, code coverage is certainly useful regardless of the percentage you're striving to achieve.)

Best of all about all this is that everything you see above is available out of the box in NetBeans IDE 7.2. Take a look at what else NetBeans IDE 7.2 brings for the first time to the world of Maven:

http://wiki.netbeans.org/NewAndNoteworthyNB72#Maven

Join the discussion

Comments ( 3 )
  • guest Wednesday, June 20, 2012

    I don't get the code coverage item shown although I opened my existing maven project with NB 7.2 Beta and added that section to the POM..

    Any ideas ?


  • Geertjan Wednesday, June 20, 2012

    My idea would be to read the blog entry again, especially the part that states: "It isn't part of NetBeans IDE 7.2 Beta, so don't even try there; you need some development build from after that."


  • guest Friday, September 21, 2012

    Hi, will you describe how to set pom.xml for Maven NetBeans Application? I tried

    step by step to add jacoco plugin in parent, app and finally module pom.xml, however, jacoco did not create any result.

    Thanks


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.