How to ignore Code Coverage for Lombok generated code?

Recently I started using lombok code in one of my projects.

It reduced verbosity to a great extent .

But one issue kept popping up. The Code Coverage tool which I was using (JaCoCo) was showing zero percentage code coverage for the domain classes annotated with @Data annotation provided by lombok.

I tried writing junit test cases by manually setting values for the setter methods.

Still it didn’t improve.

And then I researched and found out a solution.

The solution is to make lombok add @lombok.Generated annotation for all the code that is generated by lombok.

That is automatically generated if we include the following property:

lombok.addLombokGeneratedAnnotation = true

This has to be placed in a file named lombok.config at the root of the project. (Example : At the same place where pom.xml is placed in case of a Maven project)

Once that is done , Lombok automatically adds the annotation @lombok.Generated at all necessary places as mentioned before. JaCoCo then ignores all the code marked with that annotation.

That’s it.

My Code Coverage got increased.

If you have more than one lombok configuration file you can add the below property in the config file:

config.stopBubbling = true

This will stop lombok from looking at any config file in parent directories.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s