Skip to content
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

Closed
TeresaKay1224 opened this issue Dec 10, 2015 · 3 comments
Closed

Add ability to chain returns with doReturn() #599

TeresaKay1224 opened this issue Dec 10, 2015 · 3 comments

Comments

@TeresaKay1224
Copy link

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();

@johanhaleby
Copy link
Collaborator

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.

gauee added a commit to gauee/powermock that referenced this issue Dec 11, 2015
gauee added a commit to gauee/powermock that referenced this issue Dec 11, 2015
gauee added a commit to gauee/powermock that referenced this issue Dec 11, 2015
johanhaleby added a commit that referenced this issue Dec 12, 2015
#599 Add ability to chain returns with doReturn()
@thekingn0thing
Copy link
Member

Pull request is merged, so I think the issue could be closed.

@jeff-bowman
Copy link

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:

doReturn(1, 2, 3).when(Counter.class, "count");

The original StackOverflow answer I gave deals with chained return values:

doReturn(1).doAnswer(someAnswer).when(Counter.class, "count");

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants