I have two classes:
public class BaseClass <T> {
private final T config;
...
@NotNull
public final T getConfig() {
return config;
}
}
public class DerivedClass extends BaseClass<MyConfig> {
...
}
And a test:
...
MyConfig myConfig = mock(MyConfig.class);
DerivedClass child = mock(DerivedClass.class);
when(child.getConfig()).thenReturn(myConfig); // this line generates the error
I get an error that getConfig()
is returning null, rather than myConfig
. I have other method calls to the same mock object working as expected with the when/return pattern, so I played around with the polymorphism. When I removed the final restriction on the method and overrode it in the derived class (just calling the super version), the mock worked properly.
I don't want to have to redesign the product code, and reduce the rigidity of the API, for a test, so the inheritance change above isn't an acceptable solution. How can I mock out the call to the superclass' method?