I have a class (class B) that's extending a parent class (class A). The parent class has a public method (method X) that calls a method of one of its private member fields (field f > method Y). I'm trying to write a unit test to test a method of class B. However, I have to mock the call to method X that gets inherited by class B. It however gives me a null pointer error when it tries to call method Y in method X. I'm not sure how to mock field f from class B. Could someone please help me with this?
I'm using Mockito to write the test. Field f is autowired. I'd like to avoid adding new code to the file, since it's not my code. I did try:
@Mock
field f;
doReturn(someReturnValue).when(f).Y();
But this doesn't get called. I have a feeling it's because there is no field f in class B.
The current setup is something like this:
class A{
@Autowired
private Object f;
public void X(){
Map<String, Object> map = new HashMap<>();
map.putAll(f.Y()); // NPE thrown here
}
};
class B extends A{
};
Trying to mock field f in my unit test throws a null pointer error at the line where the f.Y() call is made.