0

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 setUpmethod 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

ifeins
  • 869
  • 1
  • 9
  • 20

1 Answers1

0

Looks like you only want to test the filter function.

If that is the case, you should use a Mock class to represent the mContext. And populate the values in it using your setup function.

If you don't know what Mocking is: What is Mocking?

Community
  • 1
  • 1
  • I rather do an integration test that will give me more confidence that my class works properly. – ifeins Apr 18 '15 at 09:41
  • Sorry for the late response, this might not be what you are looking for, but have you checked out: http://selendroid.io/ https://code.google.com/p/robotium/ I haven't tried these out before, but I did try selenium for web applications which seemed to have worked fine. – brian_lee Apr 24 '15 at 13:47