I have been trying to stub the superclass method call from the subclass overridden method, but till now I am stuck without any luck to succeed. I have extensively searched on the google and SO questions as well.
Here is the test code that I am using. The problem is that, in my case, both the superclass and subclass methods are getting stubbed.
@Test(enabled = true)
public void superclassMockTest() throws Exception {
ChildClass adc = getChildClass ();
doReturn(getReturnObject())
.when((SuperClass) adc).getObject(any(String.class), any(String.class), any(Map.class))
ResultObject result= adc.getObject("acc", "abc", null);
assertNotNull(result);
assertNotNull(result.getPropertyValue("attribute"));
}
The property is set on the ResultObject
in the Subclass's getObject(...) method
. I want to stub the super.getObject(...) call within the subclass's to return some arbitrary object
which is provided by getReturnObject() method
.
The problem that is occurring is that: even the call ResultObject result= adc.getObject("acc", "abc", null);
is getting stubbed and the property is not getting set, which is causing the problem.
I even tried adding: doCallRealMethod().when(adc).getObject(any(String.class), any(String.class), any(Map.class));
just before the actual call on the instance, hoping that the actual method on the instance is called. But in this case, the super.getObject(...) is not getting stubbed and getting executed.
It's kind of either or situation into which I am stuck, I can either stub both or can't stub any. Please help!