dhamini-poornachandra / mockito

Automatically exported from code.google.com/p/mockito
0 stars 0 forks source link

Enhance Mockito so that we can return values from mocks based on input values #281

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
It would be nice to have a mocked method be able to return values based on the 
arguments that are passed to it.

Here is an example implementation that is not very pretty but at least should 
show how the feature might work.

when(someMock.callSomeMethod(any(MyMessage.class)).thenReturn(
  new MockReturn() {
     public Object return() {
        MyMessage m = (MyMessage) this.args.get(0);
        m.setX("Y");
        return m;
     }
}

This sort of thing allows one to mock a method call that modifies an argument 
without having to mock the return value completely. In the case of a large XML 
document this is very helpful as it avoids the need to maintain large/volatile 
test fixtures.

Thanks, Tom

Original issue reported on code.google.com by tommytastic on 22 Sep 2011 at 1:37

GoogleCodeExporter commented 8 years ago
This already exists with the Answer interface.  See documentation at 
http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html under 
#11: "Stubbing with callbacks".

Original comment by kandpwel...@gmail.com on 26 Sep 2011 at 6:03

GoogleCodeExporter commented 8 years ago

Original comment by brice.du...@gmail.com on 9 Mar 2012 at 11:48

GoogleCodeExporter commented 8 years ago
Oups not a duplicate, but invalid. As the answer is given.

Original comment by brice.du...@gmail.com on 9 Mar 2012 at 11:50