title: How to Mock and Verify a ScheduledExcecutorService's method being called in child class
body: <p>I have a base class which looks something like this: </p>
  
  <pre><code>public abstract class BaseClass implements Runnable {
   final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
  
   @Override
   public void run() {
       someFunction();
   }
   protected abstract void someFunction();
  }
  </code></pre>
  
  <p>Then I have a child class something like this:</p>
  
  <pre><code>public class ChildClass extends BaseClass {
   functionNeedsToBeTested() {
    scheduler.scheduleAtFixedRate(this, 0L, 5L, TimeUnit.HOURS)
   }
   someFunction() {
    //Does Something
   }
  }
  </code></pre>
  
  <p>Now problem comes here when I try to write a test I am not able to verify the call of <code>scheduleAtFixedRate</code> method. My test looks like this:</p>
  
  <pre><code>@RunWith(MockitoJUnitRunner.class)
  public class TestClass {
   @Mock
   private ScheduledExecutorService scheduler;
  
   @InjectMocks
   private ChildClass obj;
  
   @Test
   public void testFunc() {
     obj.functionNeedsToBeTested();
     Mockito.verify(scheduler).scheduleAtFixedRate(Mockito.any(ChildClass.class, Mockito.anyLong(), Mockito.anyLong(), Mockito.any(TimeUnit.class)));
   }
  }
  </code></pre>
  
  <p>Test gives me this error:</p>
  
  <pre><code>junit.framework.AssertionFailedError:
      Wanted but not invoked:
      scheduler.scheduleAtFixedRate(
          &lt;any&gt;,
          &lt;any&gt;,
          &lt;any&gt;,
          &lt;any&gt;
       );
  </code></pre>