Verifying consecutive calls of the same method.

3,126 views
Skip to first unread message

Warren Tang

unread,
Sep 29, 2011, 12:05:13 PM9/29/11
to moc...@googlegroups.com
I'm testing a presenter and verifying a string input is not null or empty, but I got a TooManyActualInvocations error. Below is the code (slightly modified for demonstration):

  /**
   * When there is no uploaded file (fileUrl is null or empty)
   * and the sumbit button is clicked, 
   * An error is shown to the user.
   */
  @Test public void testShouldPromptUserWhenNoUploadedFile() {
    //given
    given(mockView.getFileUrl()).willReturn("").willReturn(null);
    
    //when
    events.onSubmit(); //call view.getFileUrl once
    events.onSubmit(); //call view.getFileUrl twice
    
    //then
    verify(mockView).setMessage(contains("empty url error")); //verify the first call >> Throws the TooManyActualInvocations exception.
    verify(mockView).setMessage(contains("null url error"));  //verify the second call
}

Brice Dutheil

unread,
Sep 29, 2011, 1:05:31 PM9/29/11
to moc...@googlegroups.com
Hi,

I see no reason why mockito would be wrong here.
Verify your code, setMessage() is probably called two or more times with a string containing "empty url error".
The TooManyActualInvocations stacktrace will show you where the first unexpected call was made.


-- 
Brice


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/8yZIF_dBKt0J.
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.

Warren Tang

unread,
Sep 29, 2011, 9:54:08 PM9/29/11
to moc...@googlegroups.com
Thanks Brice. You helped me understand the problem. The sample I posted is not exactly what I intended to be; the following is:

  @Test public void testVerifyConsecutiveCalls() {
    List mock = mock(List.class);
    when(mock.get(0)).thenReturn("a").thenReturn("b");

    mock.get(0); //first call
    mock.get(0); //second call
    
    verify(mock).get(anyInt()); // I want this line to match the first call
    verify(mock).get(anyInt()); // I want this link to match the second call
  }

The problem is that the first "verify" matches both calls instead of only one. I could change it to:

    order.verify(mock, times(2)).get(anyInt());
    //order.verify(mock).get(anyInt());

I guess mockito does not work like "match only one and then leave the rest to the other versifiers ".

Szczepan Faber

unread,
Sep 29, 2011, 9:59:02 PM9/29/11
to moc...@googlegroups.com
>I guess mockito does not work like "match only one and then leave the rest to the other versifiers ".

That's right. That was our design decision. We wanted the verification/assertion to be explicit, e.g.

verify(foo, times(1)).bar() //really means you have one interaction

verify(foo, times(2)).bar() //really means you have 2 interactions

If you don't care about the numer, use atLeastOnce()

Hope that helps!

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To view this discussion on the web visit https://groups.google.com/d/msg/mockito/-/2NXC-A9Z0HEJ.

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.



--
Szczepan Faber
Principal engineer@gradleware
Lead@mockito

Warren Tang

unread,
Sep 29, 2011, 10:11:37 PM9/29/11
to moc...@googlegroups.com
Thanks for the explanation, Szczepan.
Reply all
Reply to author
Forward
0 new messages