title: Invalid use of argument matchers! 2 matchers expected, 1 recorded
body: <p>I am writing a test to validate creations of trolleys. So,it is just a service method which basically creates number of trolleys in the database. I am mocking the repository using mockito.</p>
  <p>So, What i am doing is I am mocking and save and getAll functionality of Repository.</p>
  <p>Here is what my code looks like:-</p>
  <pre><code>package com.service;
  
  import com.model.Bin;
  import com.model.Trolley;
  import com.model.dao.NewTrolleyDao;
  import com.model.enums.BinType;
  import com.model.enums.TrolleyType;
  
  import com.repository.TrolleyRepository;
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.Mockito;
  import org.mockito.invocation.InvocationOnMock;
  import org.mockito.stubbing.Answer;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.boot.test.mock.mockito.MockBean;
  import org.springframework.data.domain.Page;
  import org.springframework.data.domain.PageImpl;
  import org.springframework.data.domain.PageRequest;
  import org.springframework.data.domain.Pageable;
  import org.springframework.test.context.junit4.SpringRunner;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.mockito.ArgumentMatchers.any;
  
  @RunWith(SpringRunner.class)
  @SpringBootTest
  public class TrolleyServiceTests {
  
      @MockBean
      private TrolleyRepository trolleyRepository;
  
      private TrolleyService trolleyService;
  
      private static final long SH_CODE = 123;
      private static final TrolleyType trolleyType = TrolleyType.QS;
      private List&lt;Trolley&gt; trolleyList = new ArrayList&lt;&gt;();
  
      @Before
      public void setUp() {
          Answer&lt;Trolley&gt; answer = new Answer&lt;Trolley&gt;() {
              public Trolley answer(InvocationOnMock invocation) {
                  Trolley trolley = invocation.getArgument(0, Trolley.class);
                  trolleyList.add(trolley);
                  System.out.println(trolleyList.size());
                  return trolley;
              }
          };
          Mockito.when(trolleyRepository.findAllByStoreHouseCode(SH_CODE, any(PageRequest.class))).
                  thenReturn(getTrolleyList());
          Mockito.when(trolleyRepository.save(any(Trolley.class))).
                  thenAnswer(answer);
          trolleyService = new TrolleyService(trolleyRepository);
      }
      private Page&lt;Trolley&gt; getTrolleyList() {
          Page&lt;Trolley&gt; trolleys = new PageImpl&lt;Trolley&gt;(trolleyList);
          return  trolleys;
      }
      private NewTrolleyDao getNewTrolleyDao(int size) {
          NewTrolleyDao newTrolleyDao = new NewTrolleyDao();
          newTrolleyDao.setStoreHouseCode(SH_CODE);
          newTrolleyDao.setType(trolleyType);
          newTrolleyDao.setSize(size);
          return newTrolleyDao;
      }
  
      @Test
      public void testCreateTrolleys() {
          NewTrolleyDao trolleyDao = getNewTrolleyDao(30);
          Page&lt;Trolley&gt; res = trolleyService.createTrolleys(trolleyDao);
          assertThat(res.getTotalElements()).isEqualTo(30);
      }
  
  }
  </code></pre>
  <p>So initially i am just taking an empty array list and mocking the save method to add to the list. Because service method calls the save method which takes a trolley and save it to db for each trolley it creates and it returns Page Of Trolley which calls the findAllByStoreHouseCode which takes storehousecode and a PageRequest.</p>
  <p>Error occurs on the line where i am using the <strong>any</strong> ArgumentMatcher.
  I am new to testing could any one please tell me where i am wrong or this approach is wrong and i should implement test in some other way.</p>
  <p>Here is the error:-</p>
  <pre><code>-------------------------------------------------------------------------------
  -------------------------------------------------------------------------------
  Test set: com.service.TrolleyServiceTests
  -------------------------------------------------------------------------------
  Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 2.948 s &lt;&lt;&lt; FAILURE! - in com.service.TrolleyServiceTests
  testCreateTrolleys  Time elapsed: 0.037 s  &lt;&lt;&lt; ERROR!
  org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
  
  Invalid use of argument matchers!
  2 matchers expected, 1 recorded:
  -&gt; at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)
  
  This exception may occur if matchers are combined with raw values:
      //incorrect:
      someMethod(anyObject(), &quot;raw String&quot;);
  When using matchers, all arguments have to be provided by matchers.
  For example:
      //correct:
      someMethod(anyObject(), eq(&quot;String by matcher&quot;));
  
  For more info see javadoc for Matchers class.
  
      at com.service.TrolleyServiceTests.setUp(TrolleyServiceTests.java:52)
  </code></pre>