15

How would i exclude inputStream.close() from jacoco code coverage, in pom.xml or in the java code?

public void run() {
    InputStream inputStream = null;
    try {
        inputStream = fileSystem.newFileInputStream(file);
    }
    finally {
        if(inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {}
        }
    }
}
nommer
  • 2,730
  • 3
  • 29
  • 44

5 Answers5

15

As for now, there's no ability to exclude a specific line (see the link):

As of today JaCoCo core only works on class files, there is no source processing. This would require a major rework of the architecture and adds additional configuration hassles.

It means, Jacoco analyzes byte code of your program, not your sources, as the result it cannot use hints like comments.

Follow the corresponding issue to track the status of such feature implementation.

As a workaround you can put it into a separate method, but see, it's a bad smell when you change your code just to reach 100% coverage level.

GoodDok
  • 1,770
  • 13
  • 28
  • Maybe [#15](https://github.com/jacoco/jacoco/issues/15) is closer, though. – Andrey Tyukin Sep 05 '19 at 17:34
  • 1
    Well, IMHO the closest one is [#794](https://github.com/jacoco/jacoco/issues/794), and since it was closed as duplicate to [#14](https://github.com/jacoco/jacoco/issues/14), I'd expect to see the related updates there. – GoodDok Sep 05 '19 at 17:50
6

I suspect what you're really aiming for is 100% coverage. Consider re-writing the code using a try-with-resources block instead. For example:

try (final InputStream inputStream = new FileInputStream(file)){
    //work with inputStream; auto-closes
}
catch (final Exception ex){
    //handle it appropriately
}
Domenic D.
  • 5,276
  • 4
  • 30
  • 41
  • 1
    You don't even need a `catch` block when using try-with-resources. – daniu Sep 04 '19 at 16:38
  • Correct. I was just trying to subtly suggest that the empty catch block in the question is not a good practice and any exceptions should be handled properly. – Domenic D. Sep 05 '19 at 03:26
  • The question title, the question body, as well as the additional clarifying question in the bounty message, all three are asking "How do I exclude a line in JaCoCo". None of them ask anything about `try-catch` or what not, it was just an example. – Andrey Tyukin Sep 05 '19 at 16:28
  • @AndreyTyukin the original question body concretely asks about how to "exclude `inputStream.close()`", that makes the question an X-Y problem solved by this answer. – daniu Sep 05 '19 at 17:51
  • 1
    @daniu I'd guess that the OP mentioned `inputStream.close()` only because there are no line numbers shown in code snippets on SO, otherwise it would be something like *"how to exclude line 8"*, but OK. Probably this answer will be helpful for someone else, it's just not exactly what I was looking for when I offered the bounty. – Andrey Tyukin Sep 05 '19 at 21:03
1

I don't think there is a way to exclude a particular statement. However, there is a provision to exclude a method, though its not recommended.As a bad workaround we can create method out of statement. The new feature has been added in the 0.8.2 release of JaCoCo which filters out the methods annotated with @Generated. For details please see the documentation below:

Classes and methods annotated with runtime visible and invisible annotation whose simple name is Generated are filtered out during generation of report

Refer https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering for more information.

Ratish Bansal
  • 1,982
  • 1
  • 10
  • 19
  • It is sufficient that the annotation contains "Generated", see https://stackoverflow.com/a/66918619/1672678. – anre Apr 21 '21 at 20:17
0

Well you can't. If you want to exclude from coverage some line which drops down the class coverage below some mandatory limit, then just exclude whole class or package. You can find more information here: Maven Jacoco Configuration - Exclude classes/packages from report not working

n1t4chi
  • 502
  • 2
  • 12
0

Possibly make a line to ignore a separate method. You can exclude a method by annotating it.

I used this post: How would I add an annotation to exclude a method from a jacoco code coverage report?

because I wanted to exclude the initBinder() Spring callback in a RestController.

So I have this:

  @ExcludeFromJacocoGeneratedReport
  @InitBinder
  private void initBinder(WebDataBinder binder) {
      binder.setValidator(myInjectedDtoValidator);
  }
Randy
  • 729
  • 5
  • 14