Trying to mock method that returns array

3,797 views
Skip to first unread message

KARR, DAVID

unread,
Jan 21, 2013, 3:51:09 PM1/21/13
to moc...@googlegroups.com
I'm trying to mock a method that returns an array. I discovered http://code.google.com/p/mockito/issues/detail?id=285 , which gives some clues about how to do this, but I can't connect the dots. This seems to be the solution for mocking a method that returns a collection, whether the data you want to simulate is an array or collection. That's not quite what I need. I'm currently using PowerMockito for these specific tests, but I don't think that helps me here.

Adrian Elsener

unread,
Jan 22, 2013, 1:55:57 AM1/22/13
to moc...@googlegroups.com
I think this shouldn't be as magic as you think.
I just did a small sample which works imho:
 
public class MockitoSamples {
   public static void main(String[] args) {
      ToBeMocked mock = Mockito.mock(ToBeMocked.class);
      when(mock.foo()).thenReturn(new String[] { "1", "2" }, new String[] { "9" });
      for (String string : mock.foo()) {
         System.out.println(string);
      }
      System.out.println(mock.foo());
      for (String string : mock.foo()) {
         System.out.println(string);
      }
      System.out.println(mock.foo());
   }
   public interface ToBeMocked {
      public String[] foo();
   }
}
 
Cheers Adrian

 
On 21 January 2013 21:51, KARR, DAVID <dk0...@att.com> wrote:
I'm trying to mock a method that returns an array.  I discovered http://code.google.com/p/mockito/issues/detail?id=285 , which gives some clues about how to do this, but I can't connect the dots.  This seems to be the solution for mocking a method that returns a collection, whether the data you want to simulate is an array or collection.  That's not quite what I need.  I'm currently using PowerMockito for these specific tests, but I don't think that helps me here.

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To post to this group, send email to moc...@googlegroups.com.
To unsubscribe from this group, send email to mockito+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mockito?hl=en.


KARR, DAVID

unread,
Jan 22, 2013, 9:54:13 AM1/22/13
to moc...@googlegroups.com

I must be missing something.  The “foo()” method returns “String[]”, but your “when” clause is having it return two String arrays.  Does it concatenate them?  How do I make it return a single array of String?

KARR, DAVID

unread,
Jan 22, 2013, 11:44:30 AM1/22/13
to moc...@googlegroups.com
> -----Original Message-----
> From: moc...@googlegroups.com [mailto:moc...@googlegroups.com] On Behalf Of
Never mind, I think I got it working. I wasn't reading the error message correctly.

Adrian Elsener

unread,
Jan 22, 2013, 4:16:57 PM1/22/13
to moc...@googlegroups.com
It's just to show how to give two different return values.
If you just need one array, just stub one ;)
Reply all
Reply to author
Forward
0 new messages