title: Mockito: Verify if Spring Data JPA delete()-method is called
body: <p>So, I am relatively new to unit-testing and especially mockito and am trying to figure out how to test the following scenario in Spring WebMVC:</p>
  
  <p>This is my Service Class (simplified):</p>
  
  <pre><code>@Service
  public class MyServiceImpl implements MyService {
  
      @Resource
      private MyCrudRepository myCrudRepository;
  
  
      /**
       * Method to delete(!) an entry from myTable.
       *
       */
      @Transactional
      public void removeTableEntry(Long entryOid, String userId) throws Exception {
  
          if (myCrudRepository.findOne(entryOid) != null) {
              myCrudRepository.delete(entryOid);
              log.info("User ID: " + userId + " deleted Entry from myTable with Oid " + entryOid + ".");
          } else {
              log.error("Error while deleting Entry with Oid: "+ entryOid + " from User with ID: " + userId);
              throw new Exception();
          }
      }
  }
  </code></pre>
  
  <p>Here I call the "built-in" delete-method of Spring Data JPA crudrepository, meaning no custom implementation in the repository itself (Using OpenJPA).</p>
  
  <p>This is my simplified Test-Class:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class MyServiceImplTest {
  
      final String USERID = "Testuser";
  
      MyServiceImpl myService;
  
      @Mock
      MyCrudRepository myCrudRepository;
  
  
      @Before
      public void setUp() {
          myService  = new MyServiceImpl();
          ReflectionTestUtils.setField(myService, "myCrudRepository", myCrudRepository);
      }
  
      //works (as expected? not sure)
      @Test(expected = Exception.class)
      public void testRemoveSomethingThrowsException() throws Exception {
          doThrow(Exception.class).when(myCrudRepository).delete(anyLong());
          myService.removeSomething(0l, USERID);
      }
  
      //does not work, see output below
      @Test
      public void testRemoveSomething() throws Exception {
          verify(myCrudRepository, times(1)).delete(anyLong());
          myService.removeSomething(0l, USERID);
      }
      //...
  }
  </code></pre>
  
  <p>So, I try to verify that delete is called in <code>testRemoveSomething()</code>, but instead I get the following output:</p>
  
  <blockquote>
  <pre><code>Wanted but not invoked:
  myCrudRepository.delete(&lt;any&gt;);
  -&gt; at myPackage.testRemoveSomething(MyServiceImplTest.java:98)
  Actually, there were zero interactions with this mock.
  </code></pre>
  </blockquote>
  
  <p>And I'm nearly out of ideas why, to be honest (thinking about the <code>@Transactional</code>, perhaps? But this didn't get me to the solution, yet). May be that I'm completely wrong here (architectural, dunno) - if so, please feel free to give me a hint :)</p>
  
  <p>It would be great to get some help here! Thanks in advance.</p>