I have the following Singleton that returns an instance of a different class. I want to mock the method on that returned instance object. I've been reading on PowerMock mocking features for final classes and singletons but I don't know if my case falls under those or not. I appreciate some suggestion.
public final class SomeWrapper {
private MyActualObject MyActualObject;
private static final SomeWrapper instance = new SomeWrapper();
private SomeWrapper() {
// singleton
}
public static SomeWrapper getInstance() {
return instance;
}
public void setMyActualObject(MyActualObject MyActualObject) {
if(this.MyActualObject == null) {
this.MyActualObject = MyActualObject;
} else {
throw new UnsupportedOperationException("MyActualObject is already set, cannot reset.");
}
}
public MyActualObject getMyActualObject() {
return MyActualObject;
}
}
So now in my unit test, I wanna mock the following line:
when(SomeWrapper.getInstance().getMyActualObject().isActive()).thenReturn(false);
Should I mock the SomeWrapper and MyActualObject? Any code sample as guidance?