![](/img/trans.png)
[英]How can I exclude specified methods and some code of lines in a method from jacoco code coverage?
[英]How to exclude a line from jacoco code coverage?
我如何從 jacoco 代碼覆蓋范圍、pom.xml 或 java 代碼中排除inputStream.close()
?
public void run() {
InputStream inputStream = null;
try {
inputStream = fileSystem.newFileInputStream(file);
}
finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {}
}
}
}
我懷疑你真正的目標是100%覆蓋率。 考慮使用try-with-resources塊重寫代碼。 例如:
try (final InputStream inputStream = new FileInputStream(file)){
//work with inputStream; auto-closes
}
catch (final Exception ex){
//handle it appropriately
}
我認為沒有辦法排除特定的陳述。 但是,有一個規定可以排除方法, though its not recommended
。作為一種不好的解決方法,我們可以創建語句之外的方法。 新功能已添加到JaCoCo的0.8.2版本中,該版本過濾掉了使用@Generated
注釋的方法。 有關詳細信息,請參閱以下文檔:
在生成報告期間過濾掉使用簡單名稱為Generated的運行時可見和不可見注釋注釋的類和方法
有關更多信息,請參閱https://github.com/jacoco/jacoco/wiki/FilteringOptions#annotation-based-filtering 。
嗯,你不能。 如果您想要從覆蓋范圍中排除一些下降類覆蓋率低於某個強制限制的行,那么只需排除整個類或包。 您可以在此處找到更多信息: Maven Jacoco配置 - 從報告中排除類/包不起作用
可能做一行來忽略一個單獨的方法。 您可以通過注釋來排除方法。
我使用了這篇文章: How would I add an annotation to exclude a method from a jacoco code coverage report?
因為我想排除 RestController 中的initBinder() Spring 回調。
所以我有這個:
@ExcludeFromJacocoGeneratedReport
@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(myInjectedDtoValidator);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.