-
Notifications
You must be signed in to change notification settings - Fork 584
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to chain returns with doReturn() #599
Comments
Interesting idea! It would be great if you could contribute by trying it out and submit a pull request. If it works the way you'll describe we'll bring it in to the next release. |
#599 Add ability to chain returns with doReturn()
Pull request is merged, so I think the issue could be closed. |
Hi! I'm glad this feature request got some traction, but this is not actually the type of chaining I was talking about originally. The pull request (#599) handles multiple doReturn values specified at once, as in:
The original StackOverflow answer I gave deals with chained return values:
This is currently not possible because PowerMockitoStubber doesn't redefine the methods on the Stubber interface to return PowerMockitoStubber instead. You should probably reopen this issue. |
I was looking for the ability to do this on StackOverflow (http://stackoverflow.com/questions/34190095/can-i-chain-returns-with-powermockito-using-doreturn) and I can't take credit for this idea, another user submitted it. But I think it would be very helpful if it is possible:
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 second doReturn in PowerMockito.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, PowerMockito doVerb 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:
public Stubber doCallRealMethod() {
answers.add(new CallsRealMethods());
return this;
}
Because this refers to the PowerMockitoStubberImpl 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:
((PowerMockitoStubber) PowerMockito.doReturn(valueA).doReturn(valueB))
.when(mockedObject, "methodName");
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:
/* in PowerMockitoStubber.java, for each doVerb method: */
@OverRide public PowerMockitoStubber doNothing();
The text was updated successfully, but these errors were encountered: