18

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) {}
        }
    }
}
7
  • What version of Java are you using? Try with resources may be an option. Commented Oct 24, 2017 at 18:44
  • @AlehMaksimovich Java 8, what do you mean by resources? Commented Oct 24, 2017 at 19:46
  • This feature: docs.oracle.com/javase/tutorial/essential/exceptions/… Commented Oct 24, 2017 at 19:53
  • unfortunately it created less code coverage :( @AlehMaksimovich Commented Oct 24, 2017 at 20:48
  • 1
    I know this has been a while, but this is the first question that pops up when searching in Google, and it still doesn't have an answer... Did you figure it out? Commented Feb 25, 2019 at 22:58

5 Answers 5

15
+100

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Maybe #15 is closer, though.
Well, IMHO the closest one is #794, and since it was closed as duplicate to #14, I'd expect to see the related updates there.
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
}

5 Comments

You don't even need a catch block when using try-with-resources.
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.
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.
@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 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.
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.

1 Comment

It is sufficient that the annotation contains "Generated", see stackoverflow.com/a/66918619/1672678.
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

Comments

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);
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.