title: Mockito change return value of a method
body: <p>In the test class trying to change the result returned from a method call.</p>
  
  <p>Trying to return true when isExistStudentInList(...) is called from the test class when testing method1.</p>
  
  <p>Tried the below but, it is actually executing the method rather than returning the true value.</p>
  
  <p>Any suggessions on how this can be done?</p>
  
  <pre><code>public class School { 
      public static void main(String[] s) {
          ..........
      }
  
      public void method1(List&lt;Students&gt; lstStudents) {
          int ilCounter       = 0; 
          .....
          while(rs.next()) { 
              ilCounter++;
  
              Students voObj = new Students();
              voObj.setName(rs.getString(1));
              voObj.setDepartment(rs.getString(2));
                  .....           
  
              boolean existStu = isExistStudentInList(lstStudents, voObj);
              if(elementId == 0 &amp;&amp; existStu) {
                  ilCounter--;    
                  .....
              } 
          }
      }
  
      public boolean isExistStudentInList(List&lt;Students&gt; lstJobElements, Students voObj) {
          boolean checkStudent;
          .........
          return checkStudent;
      }
  }
  
  public class SchoolTest { 
      @InjectMocks
      School school;
  
      .....
      @Before
      public void setUp() {
          ........
      }
  
      @Test
      public void testMethod1() throws SQLException {
          ........    
          when(school.isExistStudentInList(getSampleDataForStudents(), (Students)getSampleDataForStudents().get(0))).thenReturn(true);
          school.method1(getSampleDataForStudents());         
          ....
      }
  
      private List&lt;Students&gt; getSampleDataForStudents() {
          List&lt;Students&gt; lstStudents = new ArrayList&lt;Students&gt;();
  
                  Students student1 = new Students();
          student1 .setName("aaaa");
          student1 .setDepartment("1222");
          .....       
  
          Students student2 = new Students();
          student1 .setName("bbbb");
          student1 .setDepartment("1222");
          .....
  
          lstStudents.add(student1);
          lstStudents.add(student2);
  
          return lstStudents;
      }
  }
  </code></pre>