Issue
What's the preferred way to mock/stub objects in robolectric tests?
My goal is to write Android code in TDD manner. Right now I am using interfaces and create mock classes that implement those interfaces in tests. This process is time consuming. Is there an easier way to stub a method of existing class in robolectric?
Solution
I can only post my opinion about that topic.
My preferred way is the MVP pattern.
- java classes where you mock away views (android stuff) and test pure logic
- android classes where i mock my presenter and similar classes, the initialisation will be done by robolectric with just create phase. most view methods are just delegator.
the second point works great because robolectric deliver already "stub" classes. all what i can't check with pure android will be checkable with shadow classes.
But maybe you just search for the Shadow classes functionality, http://robolectric.org/custom-shadows/ where you can stub methods.
Update: here is also an example how I do it https://github.com/nenick/android-gradle-template/
Answered By - nenick
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.