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?