Mockk mocking Java "enum" singleton

Viewed 492

I'm working in a project which has some Java legacy code. One common pattern there is to create singletons using the "enum" approach:

enum MySingleton {
   INSTANCE;

   int answer;
   init {
     answer = 42;
   }

   int fun1() { return answer };

This INSTANCE object in reality has some complex dependencies which I cannot fulfill during unit tests. I thus would like to return a mocked MySingleton object whenever the caller uses MySingleton.INSTANCE. Is that possible?

2 Answers

No, it isn't. You can't mock enum instances.

you can mock enums using mockkObject(Enum)

Related