如何获得有关枚举单例的完整艾玛覆盖率报告?

发布于 2024-11-29 15:10:32 字数 310 浏览 3 评论 0原文

在 Java 中可以通过枚举来保证对象的唯一实例,如下所示:

public enum EmmaTest {

    ;

    public static int someStaticMethod() {
        return 33;
    }

}

如何对此类对象实现 100% Emma 测试覆盖率?是否可以?或者是否可以告诉艾玛忽略某些方法?

我能得到的最好的是:

在此处输入图像描述

It is possible to guarantee a unique instance of an object with enums in Java as following:

public enum EmmaTest {

    ;

    public static int someStaticMethod() {
        return 33;
    }

}

How can one implement 100% Emma test coverage on such objects? Is it possible? Or is it possible to tell Emma to ignore some methods?

The best I can get is:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦爱 2024-12-06 15:10:32

将下面的行添加到任何测试中都可以修复我的代码覆盖率:

MyEnum.valueOf(MyEnum.VALUE.toString());

显然,对此价值的争论与实际解决方案不同。我也有 100% 覆盖率的要求,但由于未调用枚举的构造函数,该要求下降了。将上述内容添加到测试中解决了对我来说没有任何巧妙的反思等......

Adding the line below to any test fixed it the code coverage for me:

MyEnum.valueOf(MyEnum.VALUE.toString());

Clearly the debate on the value of this is different to the actual solution. I too have a requirement for 100% coverage which was falling down due to the constructor of the enum not being called. Adding the above to a test resolved that for me without any clever reflection etc...

单身狗的梦 2024-12-06 15:10:32

您的 EmmaTest 不是单例。 EmmaTest 有 0 个实例,因此从未使用过其构造函数,并且无法使用有效值调用 valueOf。

顺便说一句:您真的担心 valueOf 或默认构造函数可能有错误吗?为什么想要 100% 的覆盖率?

Your EmmaTest is not a singleton. There is 0 instance of EmmaTest, so its constructor is never used, and there is no way to call valueOf with a valid value.

BTW: do you really fear that valueOf or the default constructor might have a bug? Why do you want 100% coverage?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文