54

I'm verifying with mockito that a method has been called. The method:

public void createButtons(final List<Button> buttonsConfiguration) {...}

Since It doesn't matter which list is passed I verify that the method is called as follows:

verify(mock).createButtons(Matchers.anyListOf(Button.class));

But, the size of the List is important. So, it doesn't matter which List but the list has to have X elements.

Is that possible at all?

iberbeu
  • 15,295
  • 5
  • 27
  • 48

4 Answers4

86

One way is to use a Captor

ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
verify(mock).createButtons(captor.capture());
assertEquals(x, captor.getValue().size()); // if expecting single list
assertEquals(x, captor.getValues().size()); // if expecting multiple lists

See http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#15 for the documentation.

You could also use a custom argument matcher. The documentation shows an example that does exactly what you want:

http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentMatcher.html

 class IsListOfTwoElements extends ArgumentMatcher<List> {
     public boolean matches(Object list) {
         return ((List) list).size() == 2;
     }
 }
 
 List mock = mock(List.class);
 when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);
 mock.addAll(Arrays.asList("one", "two"));
 verify(mock).addAll(argThat(new IsListOfTwoElements()));

You could, for instance, also add a constructor so you can specify list size desired, etc.

rogerdpack
  • 62,887
  • 36
  • 269
  • 388
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 4
    Future readers: Beware that, despite being in the Mockito documentation verbatim, this example ArgumentMatcher isn't tolerant of `null` or non-`List` types. This can cause NPE or ClassCastException. A more comprehensive solution may use `TypeSafeMatcher`, or a guard like `if (!(list instanceof List)) return false`. – Jeff Bowman Mar 10 '16 at 18:01
  • this ArgumentCaptor captor = ArgumentCaptor.forClass(List.class); cannot accept a typed list no? something like ArgumentCaptor List – Shilan Jun 19 '18 at 13:58
  • Tedious. See gertas' answer below – vikingsteve May 13 '20 at 12:24
44

With Mockito 3.x, and 2.1+ you can use Java 8 lambda expressions:

verify(mock).createButtons(argThat(list -> list.size() == 5));

With Mockito 2.1 and below similar:

verify(mock).createButtons(argThat(list -> ((List) list).size() == 5));

To check emptiness it is even easier:

verify(mock).createButtons(argThat(List::isEmpty));
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
gertas
  • 16,869
  • 1
  • 76
  • 58
25

Hamcrest (hamcrest-library dependency) provides a simpler way.

verify(mock).addAll((List) argThat(IsCollectionWithSize.hasSize(4)));

or with static import org.hamcrest.collection.IsCollectionWithSize;

verify(mock).addAll((List) argThat(hasSize(4)));
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
mrog
  • 1,930
  • 3
  • 21
  • 28
-1

Here is the working example for me:

  List<Object> objects = mock(List.class);
  when(objects.size()).thenReturn(1000);
oguzhan00
  • 499
  • 8
  • 16