Mockito Runnable: Wanted But Not Invoked?
Solution 1:
You've caught a really fun bit of expected-but-unintuitive behavior, based on this Mockito principle: To create a spy, Mockito makes a shallow copy of the original object.
When you create the anonymous inner Runnable in the constructor, the Runnable contains an implicit reference to BbScrollView.this
, your original BbScrollView object. Then, you make a copy as you create the spy, and the reference to your original BbScrollView persists. This means that your call to checkForStopped
happens to the original object that Mockito can't observe, not the spy.
One way to fix this is to move your anonymous inner Runnable creation to your startScrollTask
method, invoked on the spy, so this
refers to the spy. When the Runnable is run, it will call methods on the spy instead of the real object, allowing Mockito to intercept and verify the call.
Post a Comment for "Mockito Runnable: Wanted But Not Invoked?"