开发者

How to get full Emma coverage report on enum singletons?

开发者 https://www.devze.com 2023-03-28 00:21 出处:网络
It is possible to guarantee a unique instance of an object with enums in Java as following: public enum EmmaTest {

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 开发者_Python百科to ignore some methods?

The best I can get is:

How to get full Emma coverage report on enum singletons?


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号