title: How can I make a Mockito mock perform different actions in sequence?
body: <p>The following code:</p>
  
  <pre><code>  ObjectMapper mapper = Mockito.mock(ObjectMapper.class);
    Mockito.doThrow(new IOException()).when(mapper).writeValue((OutputStream) Matchers.anyObject(), Matchers.anyObject());
    Mockito.doNothing().when(mapper).writeValue((OutputStream) Matchers.anyObject(), Matchers.anyObject());
  
    try {
        mapper.writeValue(new ByteArrayOutputStream(), new Object());
    } catch (Exception e) {
        System.out.println("EXCEPTION");
    }
  
    try {
        mapper.writeValue(new ByteArrayOutputStream(), new Object());
    } catch (Exception e) {
        System.out.println("EXCEPTION");
    }
  </code></pre>
  
  <p>The expected output is</p>
  
  <blockquote>
    <p>EXCEPTION</p>
  </blockquote>
  
  <p>right?</p>
  
  <p>But I get nothing</p>
  
  <p>If I then make the doThrow after the doNothing I get</p>
  
  <blockquote>
    <p>EXCEPTION<br>
    EXCEPTION</p>
  </blockquote>
  
  <p>So It looks like it is the last mock that is the one that is taken... I thought it would take the mocks in the order they're registered?</p>
  
  <p>I'm looking to produce a mock throws the exception first time and completes normally the second time...</p>