Code coverage support in Netbeans
By Martin Grebac on Aug 18, 2006
Recently while working on Netbeans support for the latest web services project WSIT which is a new generation of web services stack interoperable with Microsoft .NET and supporting latest WS-\* standards and is going to be included soon in Glassfish, I had a need to write tests for underlying work with models of wsit configuration files or wsdl files - to be able to attach correct policy assertions to right places.
I was lucky, because exactly at that time, Mikhail Kondratyev together with a team, released first version of their Code Coverage Netbeans Plugin publicly for everybody to use at http://www.nbextras.org and java.net. And, it got me. I LIKE IT A LOT! Interested why? Read below...
Usually, when you write tests, you should try to cover the code as much as possible in a reasonable time. If you write some library, or APIs (whole NetBeans code is based on public-or-not APIs) it's even more important. Another usecase is when you have some code already written and you plan some large rewrite (better performance, architecture, ...). How do you make sure the functionality stays the same? Tests. There's no other way.
Netbeans has a very good JUnit test support for a long time, so this part is covered already - writing tests is pretty easy. Also, you are able to measure the test coverage in HTML form in a percentage by using some tools like Emma. However, look here:
This is how output of Mikhail's plugin looks like. Green lines mean your code has been covered by the latest running tests. Yellow lines (not present on screenshort) were marked partially, and white lines is the work you still need to do. After you install the module, enable coverage data collection by right-clicking your project and selecting Coverage -> Activate Coverage Collection. In this version, you need to do it everytime you restart NetBeans. See screenshot:
That's it! Really!
Afterwards, simply run your unit tests, or just one unit test, any way you want. When your tests pass, open any source code you have and enjoy the view ;O).
I didn't even have to use any code-quality scanning tools, and with help of this plugin I found unused methods after refactoring the code - would you believe you can't test unused private method? ;O)).
Go there: http://unitcover.dev.java.net