These are obviously related and have some similarities, but I am not sure what sets them apart. In the robolectric documentation, it is emphasized that Shadow Classes are not Mock or Stub classes. How are these different, and how would each be used?
Note: I think in this case, a practical example ("you would use a mock class to test such-and-such, which you could not test with a shadow class because of such-and-such") more than simply an overview would be especially useful and illustrative.
Think of a simple unit test for
MyActivityclass where you will not useRobolectric.setupActivity. You write a test case where you callMyActivity.onCreateto check that some preinitialisation is done when called. This test case will fail atsuper.onCreatecall which is forced by android system.Mockwill not help because you don't use a member variable which could be mocked.A
Stubwill not help because of the inheritance you can stub the onCreate method for you activity which makes testing pointless.You miss
Spybut this will also not help because of the inheritance. With Spy can avoid the real onCreate call like stubbing but makes testing also pointless.The
Shadowcan help at this situation. This handles more like a proxy. There can be an explicit proxy for each inherited class. It can intercept each type of method call also for static methods. For you example we can create a proxy for theandroid.app.Activitywhich will shadow the onCreate method and instead of throwing exceptions it will do nothing... There you can save this event so you can later check that this super.method was called with expected arguments if necessary ;)