Issue
New to Android Unit Testing with Espresso, under @Rule, what is the purpose of creating a member variable? Does the name of the variable matter? I get the inkling that I need to tell the test unit which activity (or service, class) I'm testing, but is the variable and its scope used anywhere I need to care about?
@Rule
public ActivityTestRule<MenuActivity> mActivityTestRule = new ActivityTestRule<>(MenuActivity.class);
Solution
After doing some more practice and reserach with Android UI testing with Espresso, got many of the use cases for the @Rule variables. Of of which is the with testing Idling Resources (View and data that would happen async). Using the ActivityTestRule object (ex. mActivityTestRule) I can reference resources, fire public methods with tag @VisibleForTesting in that class.
ex.
// In the activity
@VisibleForTesting
@NonNull
public SimpleIdlingResource getmSimpleIdlingResource()
{
if (mSimpleIdlingResource == null)
{
mSimpleIdlingResource = new SimpleIdlingResource();
}
return mSimpleIdlingResource;
}
// In the Test class
// the test is run.
@Before
public void registerIdlingResource() {
mIdlingResource = mActivityTestRule.getActivity().getmSimpleIdlingResource();
}
Answered By - Andrew Lam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.