shizhz
shizhz

Reputation: 12531

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.

Upvotes: 6

Views: 3234

Answers (2)

Marek
Marek

Reputation: 748

@RajeshTV:

Instructions how to use clover-aspectj-compiler are here:

https://confluence.atlassian.com/display/CLOVER/Clover+AspectJ+Compiler

These instructions are valid for OpenClover as well. Just download the:

Next call them like this:

java -cp "clover-4.2.0.jar:clover-aspectj-compiler-1.0.0.jar:aspectjrt.jar:aspectjtools.jar" com.atlassian.clover.instr.aspectj.CloverAjc -d <output directory> <list of files>

It will produce *.class files in the specified directory as well as create clover.db database.

You have to call the command above from your Maven build, for instance by using the exec:exec goal.

Please note that the clover-aspectj-compiler does not have a dedicated Maven plugin to do this automatically, so it's your job to write the whole plumbing.

Upvotes: -1

kriegaex
kriegaex

Reputation: 67437

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.

Upvotes: 1

Related Questions