Search code examples
javaunit-testingannotationscode-coverage

Should annotations be included in code coverage?


Well the questions is fairly self-explanatory.

How do various (java) code coverage tools treat annotations? Are they considered as code and included in coverage reports? They are not "executable" exactly, so I'm a bit confused about how they would be treated.

PS: Btw I've googled it already, there isn't much on this topic!


Solution

  • The short answer is that Annotations are meta-data, so they are not part of the code that can be covered. There's a bit more to think about though. What should be included in code coverage and what could be included can be quite different, depending on the technology you use.

    Most coverage tools (I know cobertura and ECLEmma) never really look at the source code, they look at the bytecode. Not every line of something that you see in your source file corresponds to anything in the executable portions of .class file.

    Annotations make this even more complicated, because some annotations are only meta-data in the source code, some are compiled and kept in the bytecode. For sure, annotations that are just meta-data should not be considered code, so they shouldn't be included in coverage info. Even annotations that are kept in the binaries should not directly be considered code to cover. Something like

    @MyAnnotation
    class MyClass ...
    

    is not executable. The annotation placed there has no executable aspect. So it can't be covered. Up to here, this is pretty certain.

    From here on, I'm speculating a bit. Annotations can contain data fields that can be queried. I don't know whether the compiler generates getter methods for that (which in principle is code that could be covered), or if they are public fields that are read directly by callers.