首页 \ 问答 \ 在迭代枚举值时,使用JaCoCo在Sonar中进行部分分支覆盖(Partial branch coverage in Sonar with JaCoCo when iterating over enum's values)

在迭代枚举值时,使用JaCoCo在Sonar中进行部分分支覆盖(Partial branch coverage in Sonar with JaCoCo when iterating over enum's values)

在SonarQube中,我注意到了诸如构造之类的令人惊讶的行为

for (final MyEnum myEnum : MyEnum.values()) {
    ...
}

其中MyEnum是我的项目中定义的自定义enum 。 包含for语句的行显示为未覆盖的分支,即使周围的代码和循环体被标记为覆盖(实际上它们至少由一个测试运行)。

我不明白这种行为。 由于values()内部返回MyEnum[] ,因此这种for循环应该计算为等于for (int i = 0; i < values.length; i++) {其中values将是存储在values()values()的结果一个临时变量。 显然这里有一个分支,但由于数组中有超过0个元素,所以进入循环的分支和破坏它的分支都会被执行。

那么为什么我看不到这条线有完整的分支覆盖?


In SonarQube I notice surprising behavior with constructs such as

for (final MyEnum myEnum : MyEnum.values()) {
    ...
}

where MyEnum is a custom enum defined in my project. The line containing the for statement is shown as an uncovered branch even though the surrounding code and the body of the loop are marked as covered (and indeed they are run by at least one test).

I do not understand this behavior. Since values() return MyEnum[], internally, this kind of for loop should evaluate to something equivalent to for (int i = 0; i < values.length; i++) { where values would be the result of values() stored into a temporary variable. Clearly there is a branch here but since there are more than 0 elements in the array, both the branch which enters the loop and the branch which breaks it get executed.

So why do I not see this line as having complete branch coverage?


原文:https://stackoverflow.com/questions/24663050
更新时间:2020-03-26 12:07

最满意答案

声纳的这一令人惊讶的结果是对我的误解。 缺乏分支覆盖是由于在这种特定方法中缺乏代码覆盖率。 由于代码根本没有被覆盖(与我在问题中首先想到和写的相反),也不是for循环中的分支。


This surprising result from Sonar was a misinterpretation on my side. Lack of branch coverage was caused by lack of code coverage in this particular method. Since the code was not covered at all (contrary to what I first thought and wrote in the question), neither could be the branch in the for-loop.

2014-07-17

相关文章

更多

最新问答

更多
  • css在元素之前中断列而不破坏包装器(css break column before element without breaking the wrapper)
  • 如何在Xamarin共享项目中使用自定义渲染器(How to use Custom Renderer in Xamarin Shared Project)
  • 如何为特定表中的特定字段设置唯一?(How to set unique for specific field from specific table?)
  • Google SDK iOS - sign()方法完成处理程序(Google SDK iOS - sign() method completion handler)
  • 在具有接口{}值的地图上实现String()(Implement String() on a map with interface{} values)
  • 检查数据库中是否已存在用户名(Check if username already exist in DB)
  • 使用javascript进行ajax调用时阻止用户交互(Block user interaction while doing ajax call using javascript)
  • 什么'if(err)'在Javascript中精确测试?(What does 'if (err)' tests precisely in Javascript?)
  • jQuery mouseleave无法正常工作(jQuery mouseleave not working)
  • 寻求使用的一些说明(Seeking some clarification on use of )
  • 将数组传递给注释的语法(syntax for passing array to annotation)
  • 用于从两个日期范围之间的文件中提取数据的Shell脚本(Shell script to extract data from file between two date ranges)
  • 元素隐藏但父()没有(Element hides but parent() not)
  • 如何使用Google App Engine Java平台开发web ui(How to develop web ui with Google App Engine Java platform)
  • 对于OWL A级;(For an OWL class A; Getting all properties that A is their domain)
  • Excel VBA公式格式问题(Excel VBA Formula Format Issue)
  • ORA - 02287序列号不允许在这里(ORA - 02287 sequence number not allowed here)
  • Github拉忽略特定文件(Github Pull Ignore Specific File)
  • SQL CONVERT函数在SQL Server中工作但不在应用程序中(SQL CONVERT function working in SQL Server but not in application)
  • backbone.js适用于大型应用程序(backbone.js for large applications)
  • 防止程序关闭(Preventing program from closing)
  • 生成不带图像的heightMap(Generating a heightMap without an Image)
  • Bootstrap - 如何将包含文本的div居中?(Bootstrap - How to center div that has text inside it?)
  • Android - 片段findViewById()总是null?(Android - Fragment findViewById() always null?)
  • 确定CSS中的高度(Figuring out heights in CSS)
  • 使用__autoload包含类和使用命名空间(Use __autoload to include class and use namespace)
  • setTimeout()不允许我传递文本值[重复](setTimeout() doesn't allow me to pass text values [duplicate])
  • 在NSUserDefault中恢复值(Restoring value in NSUserDefault)
  • 知道如何将这种下沉的悬停效果添加到图像/链接吗?(Any idea how to add this sinking hover effect to an image/link?)
  • 在XIB中淡入/淡出UISegmentedControl(fade in/fade out UISegmentedControl in XIB)