I'm using PowerMock with EasyMock, and wondered how I might mock a singleton with a private constructor?
Let's say I have the following class:
public class Singleton {
private static Singleton singleton = new Singleton();
private Singleton() { }
public static Singleton getInstance() {
return singleton;
}
public int crazyServerStuff() { ... }
}
And a class which uses this:
public class Thing {
public Thing() {}
public int doStuff(Singleton s) {
return s.crazyServerStuff() + 42;
}
}
How might I mock the crazyServerStuff
method?
I've tried the following:
@RunWith(PowerMockRunner.class)
@PrepareForTest(Singleton.class)
public class ThingTest extends AndroidTestCase {
@Test
public void testDoStuff() {
MemberModifier.suppress(MemberModifier.constructor(Singleton.class));
Singleton mockSingleton = PowerMock.createMock(Singleton.class);
...
}
}
But I get the error java.lang.IllegalArgumentException: No visible constructors in class Singleton
Does anyone know what I'm missing?