mlinhard / mockito

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

Can't use thenReturn() with arrays #285

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I want to return consequently elements of array. But signature of method 
thenReturn(T value, T... values) doesn't allow me to do this.

So I'd like to see method thenReturn(T... values) in OngoingStubbing. Method 
thenReturn(Iterable<T> values) will be very useful also.

Original issue reported on code.google.com by gvozdo...@gmail.com on 29 Oct 2011 at 9:48

GoogleCodeExporter commented 9 years ago
That may not be so easy I'm afraid. Sometimes we write: thenReturn(null). If we 
introduce those new overloads, some existing tests will fail to compile because 
null can possibly match multiple signatures.

Instead, we may introduce some answers that can be useful in such cases. You 
would be able to use them like that: then(returnConsequently(array)), 
then(returnConsequentrly(iterable))

Original comment by szcze...@gmail.com on 29 Oct 2011 at 9:05

GoogleCodeExporter commented 9 years ago
This looks like it would be a nice feature to have.  But maybe with a different 
name.  The word "consequently" doesn't quite fit with how the original poster 
used it(and yes, my first language is English).  

May I suggest these method names please?

when( mock.invocation()).thenReturnElementsOf( array or iterable );

doReturnElementsOf( array or iterable ).when( mock ).invocation();

Regards,
David.

Original comment by dmwallace.nz on 2 Dec 2011 at 2:33

GoogleCodeExporter commented 9 years ago
Ok, good feedback. You think the answers is not enough and we should add new 
methods to Mockito and Stubbable (or whatever it is called these days :) 
interface?

Original comment by szcze...@gmail.com on 6 Dec 2011 at 12:13

GoogleCodeExporter commented 9 years ago
I think it would be good to have these methods; although maybe it's not a 
particularly high priority if you add the Answers.  There are two reasons why I 
think the methods would be worth adding.

(1) It's nice and consistent with the other methods in this family.

(2) Whereas it makes little difference to a power user of Mockito, I think 
having the methods makes this functionality a little more accessible to the 
users who are still learning Mockito.  There's definitely a stage when a user 
hasn't quite grasped what Answer objects are about, but is comfortable using 
thenReturn, thenThrow and so on.  A user at that stage may need this feature.

But that's just my opinion.  Others may not agree.

Regards,
David.

Original comment by dmwallace.nz on 6 Dec 2011 at 5:10

GoogleCodeExporter commented 9 years ago
I discovered during my recent research into some of the finer details of 
Mockito, that this feature ALREADY EXISTS, and seems to have existed for a 
while.  But as far as I can tell, it's not well documented.

The Answer class that you want is 
org.mockito.stubbing.answers.ReturnsElementsOf.  Its constructor has a 
Collection as an argument, so if you want to use it with an array, you'll need 
to write something like -

when( myMock.myMethod()).then( new ReturnsElementsOf( Arrays.asList( myArray 
)));

I haven't tested this, but I can't see any reason why it wouldn't work.

Hope this helps.

Szczepan, I'm not sure whether you want to close this issue now; or whether you 
want to leave it open, to add -
* some documentation about this to the main Mockito doc;
* the method versions of this Answer, as I requested above;
* a constructor for this Answer that takes an array instead of a collection.
I think all of these tasks should be extremely low priority.

Kind regards,
David Wallace.

Original comment by dmwallace.nz on 8 Dec 2011 at 5:43

GoogleCodeExporter commented 9 years ago
David, yeah actually the problem here is not really the feature but more an 
issue with the API. We don't really want to clutter the API, yet I agree that 
some feature should be made available in a more convenient way.

Original comment by brice.du...@gmail.com on 8 Dec 2011 at 9:46

GoogleCodeExporter commented 9 years ago
Yes, but I'm more concerned about the gap in the documentation than about how 
much sugar is in the API.  The original poster raised this as an issue, because 
he or she failed to find the ReturnsElementsOf class.  But it seems that nobody 
else knew about this class either; or if they did, they didn't help out here.  
I only found it by accident, nearly 6 weeks after the original posting.

So if we can't have the two easier-to-use methods added to the API, could we at 
least have ReturnsElementsOf mentioned in the javadoc for the Mockito class?

Original comment by dmwallace.nz on 9 Dec 2011 at 7:33

GoogleCodeExporter commented 9 years ago
Yes, good idea.

Basically, for various Answers, we need to find a good place in the API. It 
cannot be top level, because we'll be adding more answers on the way. Yet, it 
needs to be somewhere fairly easy to find.

David, feel free to give suggestions & contribue! :)

Original comment by szcze...@gmail.com on 9 Dec 2011 at 9:02

GoogleCodeExporter commented 9 years ago
I think this issue is a duplicate of issue 181

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

GoogleCodeExporter commented 9 years ago
Or fairly close to.

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

GoogleCodeExporter commented 9 years ago

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