Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jacoco breaks when throwing exceptions using .class #208

Open
martinmladenov opened this issue Jun 10, 2023 · 3 comments
Open

Jacoco breaks when throwing exceptions using .class #208

martinmladenov opened this issue Jun 10, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@martinmladenov
Copy link
Collaborator

A line such as this causes Jacoco to break and stop counting lines as being covered.

when(tRepository.getTripById(1L)).thenThrow(ElementNotFoundException.class);

Replacing this with an object instance fixes the problem and Jacoco then works as expected.

when(tRepository.getTripById(1L)).thenThrow(new ElementNotFoundException());
@martinmladenov martinmladenov added the bug Something isn't working label Jun 10, 2023
@cashbreaker
Copy link
Contributor

According to JaCoCo FAQ

Source code lines with exceptions show no coverage. Why?

JaCoCo determines code execution with so called probes. Probes are inserted into the control flow at certain positions. Code is considered as executed when a subsequent probe has been executed. In case of exceptions such a sequence of instructions is aborted somewhere in the middle and the corresponding lines of source code are not marked as covered.

This is probably an issue with JaCoCo. We maybe could find a solution by replacing in code .thenThrow(ExceptionName.class) by .thenThrow(new ExceptionName())

@Arraying
Copy link
Contributor

Arraying commented Jul 7, 2023

From a static analysis viewpoint, it's quite cumbersome to replace the right code. I'm not convinced there is an easy solution that will cover all the cases, and in my opinion we should redirect the efforts to:

  1. Bring this up with JaCoCo. Are there any issues relating to this topic yet? It would be great if we could get a patch.
  2. Let students know to instantiate exceptions themselves. Perhaps we could even automatically detect this, but I fear this would run into the same issues as above.

@mauricioaniche
Copy link
Contributor

mauricioaniche commented Jul 8, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants