Skip to content Skip to sidebar Skip to footer

Mockito Runnable: Wanted But Not Invoked?

I want to make sure I'm not doing something terribly wrong before I submit a bug report. This is really weird. The setup: robolectric 3.0; mockito 1.10.19 Unit under test: public

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?"