title: jUnit & Mockito - Mocked "when" triggers verification
body: <p>I'm paraphrasing my code here, but my problem is thus.</p>
  
  <p>I have a mocked class:</p>
  
  <pre><code>@Mock
  private MyClass myInstance;
  </code></pre>
  
  <p>In my code I have a call to <code>myInstance</code> that would be made during execution, so I stub it in a setUp method:</p>
  
  <pre><code>@Before
  public void setUp() {
      Mockito.when(myInstance.doSomething("Input String")).thenReutrn("Output String");
  }
  </code></pre>
  
  <p>During execution this can happen and in a test I can verify that, however there is a test where this interaction I have stubbed does not happen (do something is never called), so I set up a test with those conditions and my initial thought is to verify this interaction does not happen:</p>
  
  <pre><code>Mockito.verify(myInstance, times(0)).doSomething("Input String");
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Mockito.verifyZeroInteractions(myInstance);
  </code></pre>
  
  <p>When I run this test these verify statements fail with a message saying (again paraphrasing) no interactions were expected - pointing to these verify statements, but interactions were found. The message points to the stub in the setup method.</p>
  
  <p><strong>Question 1:</strong> The conditions in the test mean that <code>myInstance.doSomething("Input String");</code> is never called during execution, and given (at least in my head) verification is to verify what did or did not happen during execution, why do the setting up of stubs interfere with this <strong>?</strong></p>
  
  <p>The quick solution is to move this stub out of the setUp method and into the one test where this interaction is valid, which will give me the expected result from <code>Mockito.verifyZeroInteractions(myInstance);</code> in the other test.</p>
  
  <p><strong>Question 2:</strong> But what if I introduce a third test that also requires this stub...... I'd prefer not to duplicate the code that sets up the stub in this new test, and would want it in setUp to avoid this duplication. Is there a way to achieve this and still have <code>Mockito.verifyZeroInteractions(myInstance);</code> work as I expect <strong>?</strong></p>
  
  <p>Hope I've explained myself well enough here :)</p>
  
  <p>Cheers</p>
  
  <p>Scribe</p>
  
  <p><strong>EDIT: Code as requested</strong></p>
  
  <p>Class under test</p>
  
  <pre><code>public class GnerateChangeCountryList extends TagSupport implements ChangeControlConstants {
  
  private static final long serialVersionUID = -6161465612237982393L;
  private static final Logger LOGGER = LoggerFactory.getLogger(GnerateChangeCountryList.class);
  
  private Node changeControlNode;
  
  /**
   * {@inheritDoc}
   */
  @Override
  public int doStartTag() throws JspException {
  
      try {
  
          NodeIterator countriesIterator = changeControlNode.getNode(COUNTRY_NODE_NAME).getNodes();
          System.out.println("!!!!");
          List&lt;CountryChangeBean&gt; countryList = new ArrayList&lt;CountryChangeBean&gt;();
          while (countriesIterator.hasNext()) {
              System.out.println("xxxxx");
              Node country = countriesIterator.nextNode();
              // Get the properties from the nodes
              String countryLanguageCode = country.getProperty("languageCountryCode").getString();
              String countryChangeStatus = country.getProperty("chgstatus").getString();
              String countryName = country.getProperty("countryName").getString();
  
              String countryUserName = country.getProperty("userName").getString();
              String countryUserComment = country.getProperty("userComment").getString();
  
              // Need to convert the last modified date to simple date format
              // so it is handled differently
              Property lastModifiedProperty = country.getProperty("cq:lastModified");
  
              Calendar lastModifiedValue = lastModifiedProperty.getDate();
  
              SimpleDateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
              String formattedLastModifiedDate = df.format(lastModifiedValue.getTime());
  
              String showSiteValue = country.getProperty("showSite").getString();
              String showSiteString = "";
  
              if (!showSiteValue.equals("yes")) {
                  showSiteString = "Not Shown";
              }
              countryList.add(new CountryChangeBean(countryLanguageCode, countryChangeStatus, countryName,
                      countryUserName, countryUserComment, showSiteString, formattedLastModifiedDate));
              pageContext.setAttribute("countryList", countryList);
          }
      } catch (PathNotFoundException e) {
          System.out.println("#####");
          LOGGER.error("Error looking up the .\"{}\" node for the change. Exception = {}", COUNTRY_NODE_NAME, e);
      } catch (RepositoryException e) {
          LOGGER.error("Repository exception when trying to retrieve data. Exception = {}", e);
      }
      return SKIP_BODY;
  }
  
  /**
   * Sets the node representing the change in question.
   * @param changeControlNode the change node
   */
  public void setChangeControlNode(Node changeControlNode) {
      this.changeControlNode = changeControlNode;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Test Class (I've removed the other tests to try and keep this short)</p>
  
  <p>Essentially what I expect is that because my test forces a PathNotFoundException, the calls to <code>country.getProperty()</code> that I have stubbed should never happen, and debugging the code they don't, but alas the verification fails: </p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class TestGnerateChangeCountryList implements ChangeControlConstants {
  
  private static final String LANGUAGE_COUNTRY_CODE_PROPERTY = "languageCountryCode";
  private static final String LANGUAGE_COUNTRY_CODE_VALUE = "en_gb";
  private static final String CHANGE_STATUS_PROPERTY = "chgstatus";
  private static final String CHANGE_STATUS_VALUE = "outstanding";
  private static final String COUNTRY_NAME_PROPERTY = "countryName";
  private static final String COUNTRY_NAME_VALUE = "UK";
  private static final String USER_NAME_PROPERTY = "userName";
  private static final String USER_NAME_VALUE = "someone";
  private static final String USER_COMMENT_PROPERTY = "userComment";
  private static final String USER_COMMENT_VALUE = "this is a comment";
  private static final String LAST_MODIFIED_PROPERTY = "cq:lastModified";
  private static final String SHOW_SITE_PROPERTY = "showSite";
  private static final String SHOW_SITE_VALUE = "yes";
  private static final String NOT_SHOW_SITE_VALUE = "no";
  
  @Mock
  private PageContext pageContext;
  
  @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  private Node changeControlNode;
  
  @Mock
  private NodeIterator countriesIterator;
  
  @Mock(answer = Answers.RETURNS_DEEP_STUBS)
  private Node country;
  
  @Mock
  private Property lastModifiedProperty;
  
  private GnerateChangeCountryList classUnderTest;
  
  private Calendar lastModifiedValue; 
  
  @Before
  public void setUp() throws PathNotFoundException, RepositoryException {
      lastModifiedValue = new GregorianCalendar();
      lastModifiedValue.setTime(new Date());
      classUnderTest = new GnerateChangeCountryList();
      classUnderTest.setPageContext(pageContext);
      classUnderTest.setChangeControlNode(changeControlNode);
      Mockito.when(countriesIterator.hasNext()).thenReturn(true, false);
      Mockito.when(countriesIterator.nextNode()).thenReturn(country);
  
      Mockito.when(country.getProperty(LANGUAGE_COUNTRY_CODE_PROPERTY).getString()).thenReturn(LANGUAGE_COUNTRY_CODE_VALUE);
      Mockito.when(country.getProperty(CHANGE_STATUS_PROPERTY).getString()).thenReturn(CHANGE_STATUS_VALUE);
      Mockito.when(country.getProperty(COUNTRY_NAME_PROPERTY).getString()).thenReturn(COUNTRY_NAME_VALUE);
      Mockito.when(country.getProperty(USER_NAME_PROPERTY).getString()).thenReturn(USER_NAME_VALUE);
      Mockito.when(country.getProperty(USER_COMMENT_PROPERTY).getString()).thenReturn(USER_COMMENT_VALUE);
  
      Mockito.when(country.getProperty(LAST_MODIFIED_PROPERTY)).thenReturn(lastModifiedProperty);
      Mockito.when(lastModifiedProperty.getDate()).thenReturn(lastModifiedValue);
  
  }
  
  
  
  /**
   * Test when the countries node does not exist
   * 
   */
  @Test
  public void testWhenNoCountriesNodeFound() throws PathNotFoundException, RepositoryException, JspException {
  
      Mockito.when(changeControlNode.getNode(COUNTRY_NODE_NAME)).thenThrow(new PathNotFoundException());
      classUnderTest.setChangeControlNode(changeControlNode);
      int result = classUnderTest.doStartTag();
      Assert.assertEquals(TagSupport.SKIP_BODY, result);
      Mockito.verifyZeroInteractions(country);
      //Mockito.verify(country, Mockito.never()).getProperty(Mockito.anyString());
      //Mockito.verify(pageContext, Mockito.times(0)).setAttribute(Mockito.anyString(), Mockito.any(List.class));
  }
  
  }
  </code></pre>