It seems to me that building an Activity unit test with Robolectric's lifecycle utilities (starting with Robolectric.buildActivity()
) and spying on the same Activity with a Mockito spy are mutually exclusive.
Because buildActivity()
controls the construction of the Activity object, the only place to add a spy for the Activity is after calling buildActivity()
. However, the spy doesn't function properly when it's added after the fact.
This is especially true when spying for side effects of ActivityController
lifecycle methods such as create()
, start()
and resume()
. I assume this is because the ActivityController holds a reference to the "real" Activity object and not the spy that was added later.
So is there any way to spy an Activity that's being unit tested with Robolectric, such that the spy works properly when calling the lifecycle methods via Robolectric's ActivityController
?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…