title: Misplaced argument matcher detected here. You cannot use argument matchers outside of verification or stubbing in Mockito
body: <p>Out of the following two test cases in <strong>BundleProcessorTest.java</strong>, i am getting below exception, although, my first test case passes successfully.</p>
  
  <blockquote>
    <p>org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
    Misplaced argument matcher detected here:</p>
    
    <p>-> at bundle.test.BundleProcessorTest.bundlePluginShouldNotBeNull(BundleProcessorTest.java:22)</p>
    
    <p>You cannot use argument matchers outside of verification or stubbing.
    Examples of correct usage of argument matchers:
        when(mock.get(anyInt())).thenReturn(null);
        doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
        verify(mock).someMethod(contains("foo"))</p>
    
    <p>Also, this error might show up because you use argument matchers with
    methods that cannot be mocked. Following methods <em>cannot</em> be
    stubbed/verified: final/private/equals()/hashCode().</p>
    
    <p>at
    bundle.test.BundleProcessorTest.bundlePluginCollectionShouldNotBeNull(BundleProcessorTest.java:28)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
    sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)</p>
  </blockquote>
  
  <p>Please find below simplified code listing :-</p>
  
  <p><strong>BundlePlugin.java</strong> </p>
  
  <pre><code>package bundle;
  
  import java.util.List;
  
  public class BundlePlugin {
  
      private final String pluginName ;
      private final List&lt;String&gt; featureContent ;
  
      public BundlePlugin(String pluginName, List&lt;String&gt; featureContent) {
          super();
          this.pluginName = pluginName;
          this.featureContent = featureContent;
      }
  
      public String getPluginName() {
          return pluginName;
      }
  
      public List&lt;String&gt; getFeatureContent() {
          return featureContent;
      }
  }
  </code></pre>
  
  <p><strong>BundleProcessor.java</strong></p>
  
  <pre><code>package bundle;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  public class BundleProcessor {
  
      public BundlePlugin getBundlePlugin(String pluginName, Iterator&lt;String&gt; artifactIterator) {
  
          List&lt;String&gt; featureContent = new ArrayList&lt;String&gt;() ;
  
          return new BundlePlugin(pluginName, featureContent);
      }
  }
  </code></pre>
  
  <p><strong>BundleProcessorTest.java</strong></p>
  
  <pre><code>package bundle.test;
  
  import static org.junit.Assert.assertNotNull;
  import static org.mockito.Matchers.anyString;
  import static org.mockito.Mockito.mock;
  
  import java.util.Iterator;
  import java.util.List;
  
  import org.junit.Test;
  
  import bundle.BundleProcessor;
  
  public class BundleProcessorTest {
  
      BundleProcessor bundleProcessor = new BundleProcessor() ;   
  
      @Test
      public void bundlePluginShouldNotBeNull() {
  
          Iterator&lt;String&gt; artifactIterator = mock(Iterator.class) ;
          bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;
          assertNotNull( bundlePlugin );
      }
  
      @Test
      public void bundlePluginContentShouldNotBeNull() {
          Iterator&lt;String&gt; artifactIterator = mock(Iterator.class) ;
          bundle.BundlePlugin bundlePlugin = bundleProcessor.getBundlePlugin(anyString(), artifactIterator) ;
  
          List&lt;String&gt; featureContent = bundlePlugin.getFeatureContent() ;
          assertNotNull( featureContent );
      }
  }
  </code></pre>
  
  <p>How to execute this test without problem.</p>
  
  <hr>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>But if i mark the <strong>bundlePluginCollectionShouldNotBeNull</strong> test with @Ignore annotation, then first test case passes without any exception.</p>