title: Using Mockito to mock out Spring Boot repository delete call throws java.util.NoSuchElementException on get()
body: <p>I am new to Spring Boot and Mockito and having a problem mocking out a repository call in my service test. </p>
  
  <p>I have a "delete" service method call as follows that I am trying to test with Mockito by mocking out the repository calls:</p>
  
  <pre><code>public interface IEntityTypeService {
      public EntityType getById(long id);
      public EntityType getByName(String name);
      public List&lt;EntityType&gt; getAll();
      public void update(EntityType entityType);
      public void delete(long id);
      public boolean add(EntityType entityType);
  }
  
  @Service
  public class EntityTypeServiceImpl implements IEntityTypeService {
      @Autowired
      private EntityTypeRepository entityTypeRepository;
  
      @Override
      public void delete(long id) {
          entityTypeRepository.delete(getById(id));   
      }
  
      @Override
      public EntityType getById(long id) {
          return entityTypeRepository.findById(id).get();
      }
  
  ....implementation of other methods from the interface
  }
  </code></pre>
  
  <p>My repository looks as follows:</p>
  
  <pre><code>@RepositoryRestResource
  public interface EntityTypeRepository extends LookupObjectRepository&lt;EntityType&gt; {
  
  }
  </code></pre>
  
  <p>I have not implemented any of the methods in the repository as I am letting Spring Boot wire it up for me.</p>
  
  <p>My test is as follows:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  public class EntityTypeServiceTest {
      @TestConfiguration
      static class EntityTypeServiceImplTestContextConfiguration {
  
          @Bean
          public IEntityTypeService entityTypeService() {
              return new EntityTypeServiceImpl();
          }
      }
  
      @Autowired
      private IEntityTypeService entityTypeService;
      @MockBean
      private EntityTypeRepository entityTypeRepository;
  
      @Test
      public void whenDelete_thenObjectShouldBeDeleted() {                
          final EntityType entity = new EntityType(1L, "new OET");
          Mockito.when(entityTypeRepository.findById(1L).get()).thenReturn(entity).thenReturn(null);
  
          // when
          entityTypeService.delete(entity.getID());
  
          // then
          Mockito.verify(entityTypeRepository, times(1)).delete(entity);
          assertThat(entityTypeRepository.findById(1L).get()).isNull();       
      }
  }
  </code></pre>
  
  <p>When I run the test, I get an error saying "java.util.NoSuchElementException: No value present"</p>
  
  <pre><code>java.util.NoSuchElementException: No value present
      at java.util.Optional.get(Optional.java:135)
      at xyz.unittests.service.EntityTypeServiceTest.whenDelete_thenObjectShouldBeDeleted(OriginatingEntityTypeServiceTest.java:41)
  </code></pre>
  
  <p>It references the line in the test saying <code>Mockito.when(originatingEntityTypeRepository.findById(1L).get()).thenReturn(entity).thenReturn(null);</code></p>
  
  <p>The reason I think I have to mock that call out is because the delete method in the Service calls the getById() method in the same service, which in turn calls entityTypeRepository.findById(id).get()  </p>
  
  <p>It is that, that I am assuming I have to mock out on the delete.  But clearly I am wrong.  Any assistance would be appreciated.</p>
  
  <p>Many thanks</p>