title: stubbing two methods in a mock is throwing Exception using Mockito and Spring MockMVC
body: <p>I am trying to set up some simple MVC Unit Testing. What I am trying to accomplish is this:</p>
  
  <ul>
  <li>Use Spring MockMVC to test HTTP status and HTTP data returned from my controllers.</li>
  <li>My controllers have an Autowired reference to a facade which performs the actual logic.</li>
  <li>So, I create one Test class to test one controller.</li>
  <li>I want to test two methods of that controller, so I created two methods in my Test class.</li>
  <li>I've mocked my facade and inject it in my controller.</li>
  <li>So, I want to configure this mock with the return of each of the methods I am testing.</li>
  </ul>
  
  <p>I am using the following code:</p>
  
  <pre><code>public class LoginTest {
  
      MockMvc mockMvc;
  
      @Mock
      Cadastre facade;
  
      @InjectMocks
      Login controller;
  
      @Before
      public void setup() {
          MockitoAnnotations.initMocks(this);
          Mockito.when(facade.login(Mockito.any(String.class))).thenReturn(true);
          Mockito.when(facade.getUser(Mockito.any(String.class))).thenReturn(Mockito.any(UserData.class));
  
          mockMvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
      }
  
      @Test
      public void thatLoginUsesStatusOK() throws Exception {
          this.mockMvc.perform(MockMvcRequestBuilders.get(UserFixtures.loginUrl())).andExpect(MockMvcResultMatchers.status().isOk());
      }
  
      @Test
      public void thatUserDataUsesStatusOK() throws Exception {
          this.mockMvc.perform(MockMvcRequestBuilders.get(UserFixtures.getUserDataUrl())).andExpect(MockMvcResultMatchers.status().isOk());
      }
  }
  </code></pre>
  
  <p>If I try to execute this Test class, it fails with the error:</p>
  
  <pre><code>org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
  Invalid use of argument matchers!
  1 matchers expected, 2 recorded:
  -&gt; at com.example.test.api.login.LoginTest.setup(LoginTest.java:38)
  -&gt; at com.example.test.api.login.LoginTest.setup(LoginTest.java:39)
  
  This exception may occur if matchers are combined with raw values:
      //incorrect:
      someMethod(anyObject(), "raw String");
  When using matchers, all arguments have to be provided by matchers.
  For example:
      //correct:
      someMethod(anyObject(), eq("String by matcher"));
  
  For more info see javadoc for Matchers class.
  
      at com.example.test.api.login.LoginTest.setup(LoginTest.java:39)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>If I move each mocked method line (the Mock.when lines) inside its test() method, and call this test methods separately, they work. But if I try to execute the whole class, then only one of the test() methods work, and the other throws the following exception (which seems to have the same cause as above to me):</p>
  
  <pre><code>org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
  Misplaced argument matcher detected here:
  
  -&gt; at com.example.test.api.login.LoginTest.thatLoginUsesStatusOK(LoginTest.java:45)
  
  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"))
  
  Also, this error might show up because you use argument matchers with methods that cannot be mocked.
  Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
  
      at com.example.test.api.login.LoginTest.setup(LoginTest.java:37)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:601)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
      at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
  </code></pre>
  
  <p>So, I believe the cause is that I probably did not understand very well how to use Mockito. Could you please point me in the right direction?</p>
  
  <p>Thanks a lot!</p>