easymock / easymock/easymock

Incorrect behaviour for andStubReturn()

Open
#52 0 comments 0 reactions 1 assignee Claimed by @henri-tremblay View on GitHub
bug core major
Dominant language
HTML
Stars
832
Forks
303
Avg merge
5h 25m
Merged PRs (30d)
9

Description

_Migrated from:_ CodeHaus issue EASYMOCK-41
_Original reporter:_ Henri Tremblay

---

In EasyMock 1.x you could use setDefaultReturnValue() to stub a default return value for a method call but you could also change the default return value by simply calling setDefaultReturnValue() again, with a new value. We have over 20000 tests and a significant number of these make use of this "feature". Notably, in a call to reset, we often reset the default return value for various state machines, but from time to time we may need to state to be different at the start of a test. We achieve this by then setting then default value for the same method call with a new value. From EasyMock 2.x onwards, this is no longer possible to set the default return value/stubbed return value multiple times with different values between calls to reset.

I had a look at the source for 2.5.2 specifically and noticed that the MocksBehaviour.addStub() does not look for an existing expected invocation before adding a new invocation and result object. This in itself is not wrong, but the MocksBehaviour.getStubResult() is iterating through the list of stubbed values and returning as soon as it finds the first one! This would seem to indicate that there should be only one stubbed value per mock method call.

I offer the attached SVN patch file for your consideration that will enable MocksBehaviour.addStub() to determine if an existing invocation and result exists and replace the stubbed result if applicable. This would re-enable the 1.x functionality and eliminate the ability to add multiple redundant stubbed values.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.