Take the 2-minute tour ×

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

share|improve this question
Lack of special handling for enum seems like a flaw that Emma should correct. However, 100% test coverage is likely to be counterproductive for you. –  erickson Aug 11 '11 at 16:43
It is not so much the 100% I am interested in than the red I want to get rid of in the report. –  JVerstry Aug 11 '11 at 16:46

2 Answers 2

up vote 1 down vote accepted

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?

share|improve this answer
ok, so are you saying it is not possible to obtain 100% test coverage, because there is no way to create an instance of the enum? –  JVerstry Aug 11 '11 at 16:39
Yes, that's what I'm saying. –  JB Nizet Aug 11 '11 at 16:40
I am not scared of a bug in valueOf, it is just that Emma is reporting it as untested and it is untestable. Now, I could create a dummy enum value as a workaround. I am wondering if there is a better solution. –  JVerstry Aug 11 '11 at 16:44
I think you're perverting the enum concept to make a static utility class. I would just use a regular class with a private constructor for this. The private constructor would also never be called, but the code would be cleaner, IMHO. –  JB Nizet Aug 11 '11 at 17:01
I agree, this is not exactly a singleton. I am pushing the concept described at stackoverflow.com/questions/70689/… a little further. I can define static variables in the enum to imitate singletons (which I did not in the OP). Of course, I am loosing the usage of synchronized() by doing so. –  JVerstry Aug 11 '11 at 17:21

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

share|improve this answer

Your Answer


