title: Spring boot mockito mock method according to input parameters
body: <p>I have an special case that I have no idea about how to solve.</p>
  <p>I am testing a service class method with spring boot, junit and mockito. In the class method I am testing, it call an internal method also, something like this:</p>
  <pre class="lang-java prettyprint-override"><code>@Service
  public class ServiceFacadeImpl implements ServiceFacade {
  
    //Some autowire and stuffs
  
    @Override
    public List&lt;Log&gt; getObjectLog(String objectCode) {
      List&lt;Log&gt; objectLogs = objectRepository.findByObjectCode(objectCode);
      return objectLogs;
    }
  
    @Override
    public MyObjectWrapper getMyObjectsLogByExternalCode(String externalCode) {
      List&lt;String&gt; objectCodes = objectRepository.findobjectCodesByExternalCode(externalCode);
  
      if(CollectionUtils.isEmpty(objectCodes)){
        return null;
      }
  
      MyObjectWrapper wrapper = new MyObjectWrapper();
      wrapper.setExternalCode(externalCode);
      wrapper.setObjectLogs(new ArrayList&lt;&gt;());
   
      for (String objectCode : objectCodes ) {
        ObjectLogWrapper logWrapper = new ObjectLogWrapper();
        List&lt;Log&gt; logs = getObjectLog(opCode);   // Call to internal method of the class
        logWrapper.setObjectCode(objectCode);
        logWrapper.setObjectLog(logs);
        wrapper.getObjectLogs().add(logWrapper);
      }
      return MyObjectWrapper ;
    }
  }
  </code></pre>
  <p>I am trying to mock in my test this getObjectLog(opCode) when testing the &quot;getMyObjectsLogByExternalCode&quot; method to return different Log list according to the parameter received</p>
  <pre class="lang-java prettyprint-override"><code>@SpringBootTest
  @RunWith(SpringRunner.class)
  class ObjectServiceFacadeImplTest {
  
    //Some mock beans
  
    @Autowired private ServiceFacade serviceFacade;
  
  @Test
    @DisplayName(&quot;test&quot;)
    void testGetMyObjectsLogByExternalCode() {
  
      // Given
      List&lt;String&gt; mockObCodes = Arrays.asList( &quot;obcode1&quot;,&quot;obcode2&quot; );
      when(repository.findObjectCodesByexternalCode(any())).thenReturn(mockObCodes );
  
      List&lt;OperationLogDTO&gt; mockLogsOb1 = Arrays.asList( new Log(), new Log() );  //Size 2 for ob1
      when(serviceFacade.getObjectLog(eq( &quot;obcode1&quot;))).thenReturn( mockLogsOb1 );
  
      List&lt;OperationLogDTO&gt; mockLogsOb2 = Collections.singletonList( new Log() ); //Size 1 for ob1
      when(serviceFacade.getObjectLog(eq( &quot;obcode2&quot;))).thenReturn( mockLogsOb2 );
  
      // When
      MyObjectWrapper objectWrapper = serviceFacade.getMyObjectsLogByExternalCode(&quot;TESTCODE&quot;);
  
      // Then
      assertEquals( objectWrapper.getExternalCode(),&quot;TESTCODE&quot; );
      assertEquals( objectWrapper.getObjects().size(),2 );
      assertEquals( objectWrapper.getObjects().get( 0 ).getLogs().size(),2 );  //Fails because it gets the second mock return value
      assertEquals( objectWrapper.getObjects().get( 0 ).getCode(),&quot;obcode1&quot; ); //Fails because it gets the second mock return value
      assertEquals( objectWrapper.getObjects().get( 1 ).getLogs().size(),1 );
      assertEquals( objectWrapper.getObjects().get( 1 ).getCode(),&quot;obcode2&quot; );
    }
  </code></pre>
  <p>The code has been changed because of confidential stuff, but the logic of what I am trying to do I think is clear</p>
  <p>The logic basically is that I have the main object that contains secondary objects and this secondary object contains logs objects also. There is a method that gets the logs of each secondary object and another method that gives a primary object code it obtains all logs of all secondary objects associated in a wrapper object with all info.</p>
  <p>The problem here is that when the getObjectLogs is called as a mock is always returning the second case indecently of the parameter received. I always get a log lost of size one when calling it.</p>
  <p>I don't know what I am doing wrong.</p>
  <p>UPDATE:</p>
  <p>I lso have try to use a @SpyBean instead of an @Autowired for the service but again it always return the same</p>
  <pre><code>@SpyBean private ServiceFacade serviceFacade;
  </code></pre>