I have a class that filters contacts by name, email, or phone.
I wrote a test for this class that uses target context and is able to test the functionality properly.
public class ContactsManagerTest extends InstrumentationTestCase {
protected void setUp() throws Exception {
super.setUp();
mContext = getInstrumentation().getTargetContext();
mContactsManager = new ContactsManager(mContext);
}
public void testFilterWhenQueryMatchEmail() {
List<SearchableContact> contacts = mContactsManager.filter("john.smith@gmail.com");
Assert.assertFalse(contacts.isEmpty());
}
}
Now this works, problem is that the test relies on contacts present in my phone, so if someone else run this test it will fail.
What I want to do is to have the test populate contacts at the setUp method and then use those to test the ContactsManager
.
However, I don't know want the test to put garbage contacts on my phone.
Is there some way to have the test run on a separate context where I can populate contacts without having them added to my phone?
What I tried to do is change the setUp
method to to use the regular context instead of the target context, like so:
protected void setUp() throws Exception {
super.setUp();
mContext = getInstrumentation().getContext();
mContactsManager = new ContactsManager(mContext);
}
However then I get the following error: com.example.ifeins.sandbox.test from uid 10082 not allowed to perform READ_CONTACTS
.
I tried adding an AndroidManifest.xml file to app/src/androidTest/AndroidManifest.xml
with the READ_CONTACTS
permission but it had no effect.
Thanks,
Ido