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) {}
}
}
}
Starting from JaCoCo 0.8. 2, we can exclude classes and methods by annotating them with a custom annotation with the following properties: The name of the annotation should include Generated. The retention policy of annotation should be runtime or class.
Excluding code from code coverage The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests.
JaCoCo mainly provides three important metrics: Lines coverage reflects the amount of code that has been exercised based on the number of Java byte code instructions called by the tests. Branches coverage shows the percent of exercised branches in the code, typically related to if/else and switch statements.
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
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With