I'm defining custom Enums for a webservice application and using Jacoco(0.7.5) for code coverage. The problem is that jacoco also considers compiler generated code since it evaluates compiled bytecode and because of this it shows missed instructions/branches for methods like valueOf(), values() in custom Enums which are present in the code base. I would like not to have to write test-cases for these every time I define a new Enum since these are java methods only. Is there a way to do this cleanly?
Asked
Active
Viewed 1,177 times
3
-
2Not released yet, but should be available on the latest master. See https://github.com/jacoco/jacoco/issues/15 and https://github.com/jacoco/jacoco/pull/512. – shmosel Oct 02 '17 at 19:46
-
1I could't find a way so I wrote some utility test code to hit these methods of every enum in my project. btw, this issue is nothing compared to the mess jacoco make with try-with-reources coverage. – Bohemian Oct 02 '17 at 19:50
-
@Bohemian: it’s not jacoco’s fault, as with `javac`, [try with resources introduce unreachable bytecode](https://stackoverflow.com/q/25615417/2711488). As long, as we don’t discuss the inherent problems of the idea of code coverage on byte code level… – Holger Oct 04 '17 at 10:57
-
1@Holger great post, however surely jacoco can recognize this and try with resources as special cases and not penalize the coder. Personally, I move try-with-resources into a dedicated class that accepts lambdas for the blocks and then I remove that class from code coverage. It annoys me that I must do this to achieve a (deserved) high coverage. – Bohemian Oct 04 '17 at 11:58
-
1@Bohemian: there is a broader issue behind this. On the bytecode level, `finally` is implemented by code copying, so whenever it contains conditionals whose branches may not be taken in one of the code flows (normal or exceptional), you might get incomplete coverage reported. Or, even more generally, there is no mandated output format for any source code artifact, so you can never be sure that byte code fragments have to be covered. E.g. before Java 5, class literals were compiled to `Class.forName` uses, with exception handlers and lazily populated caches, resulting in branches never taken… – Holger Oct 04 '17 at 12:50
-
@Bohemian & Holger good news you guys.. jacoco version 0.8.1 solves some of our problems.. even part of bytecode for try-with-resources statements - http://www.jacoco.org/jacoco/trunk/doc/changes.html – maddy Mar 06 '18 at 16:41