艾玛对枚举类型的报道
我正在运行 EclEmma(Eclipse 的 Emma 插件),覆盖率报告仅显示我定义的 Enum 的部分覆盖率,即使它显示了 Enum 中唯一被覆盖的值。我假设支持枚举的隐含方法存在覆盖范围差距,但我不太确定。
例如,使用此枚举,EclEmma 以绿色突出显示除包声明之外的所有内容:
package com.blah;
public enum UserRole {
HAS_ACCESS
}
如果我提取该类的覆盖详细信息,我会看到以下内容:
我的问题是,使用 EclEmma 获得 Enum 类 100% 覆盖率的最佳方法是什么?
I'm running EclEmma, the Emma plugin for Eclipse, and the coverage report shows only partial coverage for an Enum I've defined, even though it shows the only value in the Enum as being covered. I'm assuming that there is a coverage gap for the implied methods that back the Enum, but I'm not quite sure.
For example, with this Enum, EclEmma highlights everything in green, except for the package declaration:
package com.blah;
public enum UserRole {
HAS_ACCESS
}
If I pull up the coverage details for the class, I see this:
My question is, what is the best way to get 100% coverage on my Enum classes using EclEmma?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您看到的是由于枚举而生成的一些隐藏字节码。
要解决此问题,请在枚举中添加对 value() 和 valueOf() 方法的调用,如 Carl Manaster 和 Peter Lawrey 之前提到的。
What you're seeing is some hidden bytecode being generated due to an enumeration.
To get rid of this issue, add a call to the values() and valueOf() methods in the enum, as mentioned earlier by Carl Manaster and Peter Lawrey.
我同意其他发帖者的观点,即 100% 的代码覆盖率可能会被误导。
但我不得不承认,对于新编写的核心代码获得 100% 的覆盖率,我感到很满意。
幸运的是,由于所有枚举都扩展相同的“类”,因此您可以通过朋友反思的一点帮助实现 100% 的目标。
只需在类中添加以下静态方法供测试人员调用,并使用 [EnumTypeName].class 作为参数。
假设这个静态函数是在一个名为“Shared”的类中实现的,您只需要为每个枚举添加这一行:
关键字是“superficial”。
I agree with other posters that 100% code coverage can be misguided.
But I have to admit to the satisfaction of getting 100% coverage on newly written core code.
Fortunately since all enums extend the same 'class', you can achieve your 100% with a little help from your friend reflection.
Just add the following static method in a class for your testers to call, using [EnumTypeName].class as a parameter.
Assuming this static function was implemented in a class called "Shared", you would only need to include this line for each enum:
The key word is 'superficial'.
我们遇到了类似的问题,编译器生成的枚举方法(例如 value())通常不会在我们的测试代码中被调用。我们通过从最终报告中过滤掉枚举对象的数量来解决这个问题。
这就是为什么我不喜欢使用代码覆盖率来衡量完整性。当我想到更好的指标时,我会告诉你。 :)
We ran into a similar issue where the compiler generated methods on enumerations, like values(), typically were not being called in our test code. We worked around the problem by filtering the numbers of our enum objects out of our final report.
This is why I don't like using code coverage as a measure of completeness. When I think of a better metric, I'll let you know. :)