如何获得有关枚举单例的完整艾玛覆盖率报告?
在 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将下面的行添加到任何测试中都可以修复我的代码覆盖率:
显然,对此价值的争论与实际解决方案不同。我也有 100% 覆盖率的要求,但由于未调用枚举的构造函数,该要求下降了。将上述内容添加到测试中解决了对我来说没有任何巧妙的反思等......
Adding the line below to any test fixed it the code coverage for me:
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...
您的 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?