eclipse Emma 对 Enum 类型的覆盖

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4512358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:32:32  来源:igfitidea点击:

Emma coverage on Enum types

javaeclipsecode-coverageemma

提问by Javid Jamae

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.

我正在运行 EclEmma,这是 Eclipse 的 Emma 插件,覆盖率报告仅显示我定义的 Enum 的部分覆盖率,即使它显示 Enum 中唯一的值被覆盖。我假设支持 Enum 的隐含方法存在覆盖差距,但我不太确定。

For example, with this Enum, EclEmma highlights everything in green, except for the package declaration:

例如,对于这个 Enum,EclEmma 以绿色突出显示所有内容,除了包声明:

package com.blah;

public enum UserRole {
 HAS_ACCESS
}

If I pull up the coverage details for the class, I see this:

如果我调出课程的覆盖范围详细信息,我会看到:

alt text

替代文字

My question is, what is the best way to get 100% coverage on my Enum classes using EclEmma?

我的问题是,使用 EclEmma 对我的 Enum 类进行 100% 覆盖的最佳方法是什么?

回答by deterb

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.

要解决此问题,请在枚举中添加对 values() 和 valueOf() 方法的调用,如前面 Carl Manaster 和 Peter Lawrey 所述。

回答by Darren

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.

我同意其他海报,即 100% 的代码覆盖率可能会被误导。但我不得不承认对新编写的核心代码获得 100% 的覆盖率感到满意。

Fortunately since all enums extend the same 'class', you can achieve your 100% with a little help from your friend reflection.

幸运的是,由于所有枚举都扩展了相同的“类”,因此您可以在朋友的反思的帮助下实现 100%。

Just add the following static method in a class for your testers to call, using [EnumTypeName].class as a parameter.

只需在类中添加以下静态方法供测试人员调用,使用 [EnumTypeName].class 作为参数。

  public static void superficialEnumCodeCoverage(Class<? extends Enum<?>> enumClass) {
    try {
      for (Object o : (Object[])enumClass.getMethod("values").invoke(null)) {
        enumClass.getMethod("valueOf", String.class).invoke(null, o.toString());
      }
    }
    catch (Throwable e) {
      throw new RuntimeException(e);
    }
  }

Assuming this static function was implemented in a class called "Shared", you would only need to include this line for each enum:

假设这个静态函数是在一个名为“Shared”的类中实现的,你只需要为每个枚举包含这一行:

Shared.superficialEnumCodeCoverage(UserRole.class);

The key word is 'superficial'.

关键词是“肤浅”。

回答by dhable

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.

我们遇到了一个类似的问题,即编译器生成的枚举方法(如 values())通常不会在我们的测试代码中被调用。我们通过从最终报告中过滤掉枚举对象的数量来解决这个问题。

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. :)

这就是为什么我不喜欢使用代码覆盖率来衡量完整性的原因。当我想到更好的指标时,我会告诉你。:)