Karthik Kota
Karthik Kota

Reputation: 423

Java code coverage missed instructions?

I'm wondering how code coverage is measured for Java. For a class my unit test covers most of the functionality but coverage result is about 44%. We use a custom tool at our company and wondering how is the coverage usually measured.

What would be the ideal code coverage percentage to have?

Upvotes: 6

Views: 15784

Answers (1)

Ashley Frieze
Ashley Frieze

Reputation: 5458

Dependent on the tool the code coverage might be measured in lines touched by tests or in the number of different branches covered by tests. The metric alone isn't very interesting - look at which lines are not covered. If you're using Eclipse or IntelliJ, there is a code coverage view which you can bring up to show you.

Emma and Cobertura are both decent code coverage tools, but will give different results on the same code+unit tests.

44% code coverage is pretty low.

If you're doing test driven development, then you should be able to achieve 90%+ code coverage, with only weird exceptions and small snippets of file/network access that you exclude from unit testing.

If you've got 44% code coverage, this may be ok. Your code might contain a lot of getters/setters which you're not using right now, but which are handy to have there. Similarly I've noticed that it's not worth exercising all routes through a hashCode or equals method just to appease the coverage tool. If your code is very POJOey with lots of uninteresting boilerplate that was auto-generated, then it's probably no big deal.

Tools like SonarQube are essential for visualising the combination of code coverage hot spots and static code analysis quality metrics.

Don't use quirky in-house tools, find something that's commonly supported.

Upvotes: 3

Related Questions