Jacoco & Gradle - How to Verify Coverage With Exclusions

A post about how to add exclusions to your Jacoco test coverage verification in Gradle.

This is how you setup Jacoco to fail when the code doesn’t meet the expected coverage threshold:

See that rule? I’m not setting any particular element so by default it’ll set BUNDLE. This is just what I want as I’d like to set a threshold for the coverage of the entire module.

So if I need to exclude certain packages or files from the count this is what I do:

Why exclude doesn’t work

My first approach was setting the excludes property on the rule like this:

The reason this doesn’t work is that excludes works on objects of the type defined with the element property. In this case the type is BUNDLE whereas the thing we want to exclude are packages and files.