I have a method call that needs to return valueA the first time it is called and valueB the second time it is called. I'm using a PowerMockito spy, so if I were just needing to return one value it would look like this:
PowerMockito.doReturn(valueA).when(mockedObject, "methodName");
It looks like I can do chained returns like this:
PowerMockito.when(mockedObject, "methodName").thenReturn(valueA).thenReturn(valueB);
But I need to indicate chained returns with doReturn so that the real methodName() isn't called on my Spy.
I've tried this, but Eclipse gives me an error saying that it won't even compile:
PowerMockito.doReturn(valueA).doReturn(valueB).when(mockedObject, "methodName");
Is it even possible to chain returns with doReturn and powermockito? If so, how?
Try this:
In short, I think you've found a hole in the PowerMockito API. This may be an excellent thing to submit as a pull request, or at least to file as a feature request.
What's happening is that
org.powermock.api.mockito.PowerMockito.doReturn
(et al) will return a PowerMockitoStubber implementation, which extends Mockito's Stubber; under the hood, PowerMockitoStubberImpl extends StubberImpl. Because PowerMock doesn't need to change the functionality, it does not override these calls; the seconddoReturn
inPowerMockito.doReturn(foo).doReturn(bar)
will invoke Mockito's StubberImpl and return a Mockito Stubber.This is a problem, because in the transition to Stubber, you lose the PowerMockito
when
signatures, such as the one you need. In short, PowerMockitodoVerb
calls do support chaining, and do support referring to Methods reflectively or by name, but currently not both at the same time.Internally, StubberImpl follows the builder pattern, returning itself after every call:
Because
this
refers to thePowerMockitoStubberImpl
subclass, it would be easy to cast the Stubber to PowerMockitoStubber to get access to the additional methods. For the above workaround, you're making the cast yourself:As a long term solution, because anything that returns PowerMockitoStubber necessarily returns Stubber, this may be possible to fix for all PowerMockito users purely through an interface override (noting the caveats listed on Joseph D. Darcy's Oracle Weblog). I haven't tested this, but it may be this easy:
At which point you would simply have to adapt the return type:
I don't think so. Rather, You can achieve it with using doAnswer and Queue like below