title: Mockito verify number of invocations in spring boot test
body: <p>I'm trying to verify that a mocked method (<code>fileSenderService.sendFile</code>) was called exactly 2 times. For whatever reason Mockito never fails the test, no matter what number of invocations are expected. I'm using it like this:</p>
  <p><code>verify(mockService, times(expectedNumberOfInvocations)).sendFile(any(String.class), any(byte[].class));</code></p>
  <p>No matter what value I use in the times() method, the test always passes.</p>
  <p>The MyService looks like this:</p>
  <pre><code>@Service
  public class MyServiceImpl implements MyService {
  
      @Autowired
      private FileSenderService fileSenderService;
  
      public void createAndSendFiles(){
          //doSomeStuff, prepare fileNames and fileContents(byte arrays)
          //execute the sendFile twice
          fileSenderService.sendFile(aFileName, aByteArray); //void method; mocked for testing
          fileSenderService.sendFile(bFileName, bByteArray); //void method; mocked for testing
      }
  </code></pre>
  <p>The test class</p>
  <pre><code>@RunWith(SpringRunner.class)
  @WebAppConfiguration
  @SpringBootTest(classes = {Application.class, FileSenderServiceMock.class})
  @ContextConfiguration
  public class MyServiceTest{
  
      @Autowired
      private MyService myService;
  
      @Autowired
      FileSenderService mock;
  
      @Test
      public void shouldCreateAndSendFiles(){
          myService.createAndSendFiles(); //inside this method sendFile() is called twice
          verify(mock, times(999)).sendFile(any(String.class), any(byte[].class)); //THE PROBLEM - why times(999) does not fail the test?
      }
  }
  
  </code></pre>
  <p>The FileSenderService and its mock:</p>
  <pre><code>@Service
  public class FileSenderServiceImpl implements FileSenderService {
     @Override
      public void sendFile(String name, byte [] content) {
        //send the file
      }
  }
  
  //used for testing instead of the actual FileSenderServiceImpl 
  public class FileSenderServiceMock {
      @Bean
      @Primary
      public FileSenderService getFileSenderServiceMock(){
          FileSenderServicemock = Mockito.mock(FileSenderService.class, Mockito.RETURNS_DEEP_STUBS);
  
          doNothing().when(mock).sendFile(isA(String.class), isA(byte[].class));
          return mock;
      }
  </code></pre>