title: Verify mock methods got called in exact order using Mockito.inOrder
body: <p>I'm trying to test the method on mocked object getting called in the expected order or not. Below is the simplified example:</p>
  
  <pre><code>@Test
  public void test() {
      List&lt;String&gt; mockedList = Mockito.mock(List.class);
  
      for (int i = 0; i &lt; 5; i++) {
          mockedList.add("a");
          mockedList.add("b");
          mockedList.add("c");
      }
  
      // I want only this to pass.
      InOrder inOrder1 = Mockito.inOrder(mockedList);
      inOrder1.verify(mockedList).add("a");
      inOrder1.verify(mockedList).add("b");
      inOrder1.verify(mockedList).add("c");
  
      // I want this to fail.
      InOrder inOrder2 = Mockito.inOrder(mockedList);
      inOrder2.verify(mockedList).add("c");
      inOrder2.verify(mockedList).add("b");
      inOrder2.verify(mockedList).add("a");
  }
  </code></pre>
  
  <p>Although the verifying order (<code>c -&gt; b -&gt; a</code>) is different from the invocation order (<code>a -&gt; b -&gt; c</code>), this test passes. This is because Mockito verifies if method2 called <strong>anywhere AFTER</strong> method1, but not immediately (i.e., no other method called in between). As I'm adding elements multiple times, this is very much possible. Which means, Mockito InOrder passes for <code>b -&gt; a -&gt; c -&gt; a -&gt; c -&gt; b -&gt; c -&gt; b -&gt; a ...</code></p>
  
  <p>But I want this to fail, and make sure the order is always <code>a -&gt; b -&gt; c -&gt; a -&gt; b -&gt; c -&gt; a -&gt; b -&gt; c ...</code></p>
  
  <p><strong>Update</strong>: Proper way to verify is to verify the order same number of iterations <em>(summary of accepted answer)</em>:</p>
  
  <pre><code>for (int i = 0; i &lt; 5; i++) {
      inOrder1.verify(mockedList).add("a");
      inOrder1.verify(mockedList).add("b");
      inOrder1.verify(mockedList).add("c");
  }
  
  // fail the test if we missed to verify any other invocations
  inOrder1.verifyNoMoreInteractions();
  </code></pre>