Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jacoco code coverage is affected by AspectJ

We're using AspectJ in our project and also Jacoco for test coverage report, currently we're facing an issue that due to AspectJ changed the byte code during compiling phase, which makes the code coverage report not correct. One example is due to AspectJ adds extra if-else statement, then the branch coverage shows something like 1/4 but actually there's no condition branch in the source code. Is there some good way to tell Jacoco to ignore all code generated by AspectJ?

Thanks a lot.

like image 279
shizhz Avatar asked Aug 14 '15 03:08

shizhz


Video Answer


1 Answers

I am copying here the answer I just wrote on the JaCoCo mailing list:

You have two options with AspectJ if you want to avoid it compiling from source:

  1. Use LTW with the weaving agent.
  2. Move your aspects into a separate Maven module. Compile your Java modules with the normal Maven Compiler Plugin and the aspect module with AspectJ Maven. Then create another module which just uses AspectJ Maven in order to do binary weaving on a Java module, using both previously created artifacts as dependencies. In this scenario you need to make sure that JaCoCo offline instrumentation is bound to a phase before binary weaving is done.

The easiest way out, though, would be to test your aspects in isolation and also the Java code without aspects and measure coverage there without any issues.

like image 79
kriegaex Avatar answered Oct 06 '22 01:10

kriegaex