Consider the following method:
public boolean isACertainValue() {
if(context.getValueA() != null && context.getValueA().toBoolean() == true) {
if(context.getType() != null && context.getType() == ContextType.certainType) {
return true;
}
}
return false;
}
I did not write this code, it is ugly as hell, it is totally overcomplicated but I have to work with it.
Now I want to test a method that relies on a call to this method.
I thought I could deal with this by:
Mockito.when(spy.isACertainValue()).thenReturn(true);
because that's the case I want to test.
But it doesn't work as it is still calling the method-body :/
I get nullpointers or rather I get something along the lines of
misusing.WrongTypeOfReturnValue; Boolean cannot be returned by getValueA(). getValueA() should return ValueA
So I tried (as a workaround) to do:
Mockito.when(contextMock.getValueA()).thenReturn(new ValueA());
and
Mockito.when(contextMock.getType()).thenReturn(ContextType.certainType);
but then I get a nullpointer that I cant seem to be able to debug.
So, how is it done right in this case?