Exclude code block from code coverage

609 views
Skip to first unread message

Omar Al-Haj Qasem

unread,
Oct 9, 2023, 7:33:10 AM10/9/23
to JaCoCo and EclEmma Users
I need to exclude code block from code covarge, not just execlude function or class.
for example, when to exclude if starement or else branch from coverage.
Is that possible? if not, is there some kind of work around to do so?

Thanks
Omar

Marc Hoffmann

unread,
Oct 9, 2023, 12:55:09 PM10/9/23
to JaCoCo and EclEmma Users
Hi,

this is not possible. You may extract the code to its own method.

Regards,
-marc

--
You received this message because you are subscribed to the Google Groups "JaCoCo and EclEmma Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jacoco+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jacoco/ad190671-52ff-4397-9cd9-41c0533ece6bn%40googlegroups.com.

Omar Al-Haj Qasem

unread,
Oct 12, 2023, 5:32:11 AM10/12/23
to JaCoCo and EclEmma Users
Thanks for the reply.
When I tried that using annotation for a function that has no Lambda expression it worked fine, but when there is a lambda expression inside the candidate function it does not filter the whole function; only the first lines that are not part of the lambda expression.
Is there a way to exclude the whole function or exclude the lambda expression inside a function?

@ExcludeFromJacocoGeneratedReport
public void handleArrivedNotifications{
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
logger.info("received order to send emails:");
// lambda expression not excluded CompletableFuture.runAsync(() -> {
try {
// body
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
});

}

Best,
Omar

Marc Hoffmann

unread,
Oct 12, 2023, 5:40:52 PM10/12/23
to JaCoCo and EclEmma Users
Hi Omar,

please consider these kind of annotations as a workaround. JaCoCo is not designed to have manual tagging.

The reason for this behaviour is that the lambda body will be compiled in another method which cannot be annotated. As another workaround you can extract the body to another (annotated) method and use a method reference to it.

Cheers,
-marc


Reply all
Reply to author
Forward
0 new messages