title: Mockito doThrow on void method with null argument not throwing exception: What am I doing wrong?
body: <p>I am unsure if I have found a bug or if I am just doing it wrong. I am trying to have a mock throw an exception when a method is invoked (relatively easy normally), except the method is of <code>void</code> return type, and the object to be passed in (why the error is thrown) is <code>null</code>. This can be typed through <code>isNull()</code> to compile, but the error is still not thrown.</p>
  
  <pre><code>package some.example;
  
  import static org.mockito.Matchers.isNull;
  import static org.mockito.Mockito.doThrow;
  import static org.mockito.Mockito.reset;
  import static org.mockito.Mockito.verify;
  import static org.mockito.MockitoAnnotations.initMocks;
  
  import javax.jms.Message;
  import javax.jms.MessageListener;
  
  import org.mockito.Mock;
  import org.springframework.jms.listener.AbstractMessageListenerContainer;
  import org.springframework.jms.listener.SessionAwareMessageListener;
  import org.testng.annotations.AfterMethod;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.Test;
  
  public class TestClass {
      @Mock
      private AbstractMessageListenerContainer messageContainer;
  
      @BeforeClass
      public void setUpMocks() {
          initMocks(this);
  
          doThrow(new IllegalArgumentException()).when(messageContainer).setupMessageListener(
                  isNull(MessageListener.class));
          doThrow(new IllegalArgumentException()).when(messageContainer).setupMessageListener(
                  isNull(SessionAwareMessageListener.class));
      }
  
      @AfterMethod
      public void resetMocks() {
          reset(messageContainer);
      }
  
      @Test(expectedExceptions = { IllegalArgumentException.class })
      public void testSetUpQueueConsumerWithNullMessageListener() throws Exception {
          final MessageListener messageListener = null;
          try (final QueueConsumer consumer = new QueueConsumer(messageContainer, messageListener)) {
          } finally {
              verify(messageContainer).setupMessageListener(messageListener);
          }
      }
  
      @Test(expectedExceptions = { IllegalArgumentException.class })
      public void testSetUpQueueConsumerWithNullSessionAwareMessageListener() throws Exception {
          final SessionAwareMessageListener&lt;Message&gt; messageListener = null;
          try (final QueueConsumer consumer = new QueueConsumer(messageContainer, messageListener)) {
          } finally {
              verify(messageContainer).setupMessageListener(messageListener);
          }
      }
  
      public class QueueConsumer implements AutoCloseable {
          private final AbstractMessageListenerContainer messageContainer;
  
          QueueConsumer(final AbstractMessageListenerContainer messageContainer,
                  final SessionAwareMessageListener&lt;? extends Message&gt; messageListener) {
              this(messageContainer);
              this.messageContainer.setupMessageListener(messageListener);
          }
  
          QueueConsumer(final AbstractMessageListenerContainer messageContainer, final MessageListener messageListener) {
              this(messageContainer);
              this.messageContainer.setupMessageListener(messageListener);
          }
  
          private QueueConsumer(final AbstractMessageListenerContainer messageContainer) {
              if (messageContainer == null) {
                  throw new IllegalArgumentException("MessageListenerContainer cannot be null");
              }
              this.messageContainer = messageContainer;
          }
  
          public void stop() {
              messageContainer.stop();
          }
  
          @Override
          public void close() throws Exception {
              stop();
          }
      }
  }
  </code></pre>
  
  <p>Relevant maven dependencies:</p>
  
  <pre><code>&lt;dependency&gt;
      &lt;groupId&gt;org.testng&lt;/groupId&gt;
      &lt;artifactId&gt;testng&lt;/artifactId&gt;
      &lt;version&gt;6.8.21&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.mockito&lt;/groupId&gt;
      &lt;artifactId&gt;mockito-core&lt;/artifactId&gt;
      &lt;version&gt;2.0.8-beta&lt;/version&gt;
      &lt;scope&gt;test&lt;/scope&gt;
  &lt;/dependency&gt;
  &lt;dependency&gt;
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
      &lt;version&gt;${org.springframework.version}&lt;/version&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>I added the stripped down real code. I toyed with the code more this morning, and discovered I was using Mockito's <code>reset()</code> incorrectly. I thought it would reset the mock to its unverified state, but it also erases the <code>doThrow()</code> stubs. I had to change the initialization methods as follows:</p>
  
  <pre><code>@BeforeClass
  public void setUpMocks() {
      initMocks(this);
  }
  
  @BeforeMethod
  public void setUpThrows() {
      doThrow(new IllegalArgumentException()).when(messageContainer).setupMessageListener(
              isNull(MessageListener.class));
      doThrow(new IllegalArgumentException()).when(messageContainer).setupMessageListener(
              isNull(SessionAwareMessageListener.class));
  }
  </code></pre>