title: findById returning empty in Junit Test case
body: <p>I am testing update Employee method using junit. Here is junit code looks like</p>
  <pre><code>    @RunWith(SpringJUnit4ClassRunner.class)
      public class EmployeeServiceTest {
  
      @InjectMocks
      EmployeeService service;
      
      @Mock
      EmployeeRepository empRepo;
      
      @Mock
      Employee emp;
      
      @Before
      public void SetupContext() {
          MockitoAnnotations.initMocks(this);
          emp = new Employee(1,&quot;Ankush&quot;,4000,&quot;Mumbai&quot;);
      }
      
      
      @Test
      public void updateEmployee() throws, Exception {
          when(empRepo.save(emp)).thenReturn(emp);
          EmployeeDTO empDTO=new EmployeeDTO(emp.getId(),&quot;Ankush&quot;,4000,&quot;Chennai&quot;);
          EmployeeDTO updatedEmp = service.updateEmployee(empDTO);
          assertThat(updatedEmp.getCity().equals(&quot;Chennai&quot;));
      }
     }
  </code></pre>
  <p>Inside <code>updateEmployee</code> service method i am having one check like</p>
  <pre><code>public EmployeeDTO updateEmployee(EmployeeDTO empDTO){
       Optional&lt;Employee&gt; existingemp = empRepo.findById(empDTO.getId());
       if(existingemp.present()){
           //  converting DTO to entity
            empRepo.save(entity); 
        }else{
           throw new EmployeeServiceException(&quot;Employee Not Found to update&quot;);
        }
      return convertEntityToDto(entity);
   }
  </code></pre>
  <p>Why <code>existingemp</code> is always empty i had already saved the object <code>when(empRepo.save(emp)).thenReturn(emp);</code> where <code>emp</code> have the id <code>1</code> only.</p>