Issue
DB : SQLite
Table : Contact
Espresso test:
@Test
public void testBlock() {
onData(anything()).inAdapterView(withId(R.id.container_ListView)).atPosition(0).onChildView(withText(R.string.block_user))
.perform(click());
}
And test success pass. But it succeeds only if before starting the contact has the status unblock (column Status in db table). So I need (before starting the test) to update this Contact to status unblock. How can I do this? Or has anyone a better solution for this?
Solution
This is something you can either do it in a @Before method or within your testcase before starting the activity.
First you need to change your activity rule to this:
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
MainActivity.class, false, false);
Note the last false flag, this says that your activities are not launched automatically when your test start.
Then in your test class you could add this method:
@Before
public static void PrepareDatabase() {
// Instantiate your Service that performs an action on the database
// give it this context: InstrumentationRegistry.getTargetContext();
// For example the code could look like this:
DatabaseHelper(InstrumentationRegistry.getTargetContext()).setYourValue();
mActivityRule.launchActivity(null); //launches the test activity
}
Then normally write your test. This code performs some operation on your database and afterwards launches your activity for the test. You can also pass this into your test method instead of in the @Before method.
Answered By - stamanuel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.