I don't understand verifyZeroInteractions

297 views
Skip to first unread message

Federico Fissore

unread,
Mar 3, 2017, 11:20:35 AM3/3/17
to mockito
Hi all

I think the code below must fail at "verifyZeroInteractions(collaborator)" but it does not. What am I getting wrong? (I'm using mockito 2.7.13)

package test;

import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

public class MockitoTest {

   
private static class Collaborator {

     
public String doStuff(int number) {
         
throw new RuntimeException();
     
}

   
}

   
private static class Container {

     
private final Collaborator collaborator;

     
public Container(Collaborator collaborator) {
         
this.collaborator = collaborator;
     
}

     
public String doStuff(int number) {
         
return collaborator.doStuff(number);
     
}

   
}


   
@Rule
   
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

   
@Mock
   
private Collaborator collaborator;

   
@Test
   
public void testVerifyZeroInteractions() throws Exception {
     
Container container = new Container(collaborator);
     
when(collaborator.doStuff(42)).thenReturn("yeah");

     
String output = container.doStuff(42);
      assertEquals
("yeah", output);

      verifyZeroInteractions
(collaborator);
   
}
}


Regards

Federico

Eric Lefevre-Ardant

unread,
Mar 7, 2017, 9:18:48 AM3/7/17
to mockito

Well, do you get the RuntimeException sent by Collaborator.doStuff()? From reading your code, it seems that you would.

In that case, the test would fail before reaching the call to verifyZeroInteractions().


--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.
To view this discussion on the web visit https://groups.google.com/d/msgid/mockito/87e23df0-60fe-43d3-b2db-8dd53731f928%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Federico Fissore

unread,
Mar 7, 2017, 9:20:46 AM3/7/17
to moc...@googlegroups.com
I don't but that's expected. Collaborator.doStuff is stubbed

Eric Lefevre-Ardant

unread,
Mar 7, 2017, 9:36:51 AM3/7/17
to moc...@googlegroups.com
Sorry, you're right, I read wrong.

Not sure why it's not behaving as you expect, then :-\

--
You received this message because you are subscribed to the Google Groups "mockito" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mockito+unsubscribe@googlegroups.com.
To post to this group, send email to moc...@googlegroups.com.
Visit this group at https://groups.google.com/group/mockito.

Federico Fissore

unread,
Mar 7, 2017, 9:46:56 AM3/7/17
to moc...@googlegroups.com
I've opened an issue with a more complete test case

Reply all
Reply to author
Forward
0 new messages