ProviderTestCase2.getProvider() is null
Asked Answered
E

2

5

I'm trying to test a ContentProvider class, and can't make it work.

getProvider() keeps returning null, but as I understand from the ProviderTestCase2.setUp() code, it shouldn't.

public class NotesProviderTest extends ProviderTestCase2<NotesProvider>
{
    ...

    public NotesProviderTest()
    {
        super(NotesProvider.class, Contract.AUTHORITY);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();

    }

    public void testNoteProvider__inserts_a_valid_record() throws Exception
    {
        Note note = new Note(new JSONObject(simpleNoteJson));

        NotesProvider provider = getProvider();

        Uri insert = provider.insert(Note.URI, note.getContentValues());
        assertEquals(1L, ContentUris.parseId(insert));

        Cursor cursor = provider.query(Note.URI, null, null, new String[]{}, null);

        assertNotNull(cursor);

        cursor.close();
    }
}

Side note: the provider works if used within the app.

Thanks in advance.

Endblown answered 16/6, 2015 at 14:38 Comment(4)
Did you add anything to your provider besides the usual query, insert... provider methods?Fingerstall
nop, nothing strangeEndblown
Have you tried several different APi levels, devices, etc?Erna
are you running this as an instrumentation test?Floss
P
20

I just ran into this issue myself. You need to tell AndroidJUnit4 to run the setUp method with the @Before annotation. If you don't do this the setUp method will not be called before your unit test runs.

The code snippet for overriding the setUp method on http://developer.android.com/training/testing/integration-testing/content-provider-testing.html is misleading and doesn't mention that you need an @Before annotation.

Try the following:

@Before
@Override
public void setUp() throws Exception
{
    setContext(InstrumentationRegistry.getTargetContext());
    super.setUp();
}
Pesky answered 16/3, 2016 at 2:11 Comment(1)
This looks like a mistake - why are they never calling setUp?Paterson
U
1

As part of the setUp() method a MockContentResolver should be created. Use this to create and inject the provider.

See class MockContentResolver: http://developer.android.com/reference/android/test/mock/MockContentProvider.html

Source of example: http://alvinalexander.com/java/jwarehouse/android/test-runner/src/android/test/ProviderTestCase2.java.shtml

Partial sample from example in link above:

 @Override
    protected void setUp() throws Exception {
        super.setUp();

        mResolver = new MockContentResolver();
        final String filenamePrefix = "test.";
        RenamingDelegatingContext targetContextWrapper = new RenamingDelegatingContext(
                new MockContext2(), // The context that most methods are delegated to
                getContext(), // The context that file methods are delegated to
                filenamePrefix);
        mProviderContext = new IsolatedContext(mResolver, targetContextWrapper);

        mProvider = mProviderClass.newInstance();
        mProvider.attachInfo(mProviderContext, null);
        assertNotNull(mProvider);
        mResolver.addProvider(mProviderAuthority, getProvider());
    }
Unmeet answered 23/6, 2015 at 19:23 Comment(1)
That's exactly my point, if ProviderTestCase2.setUp() already creates a MockcontentResolver, why should I create my own in a child class?Endblown

© 2022 - 2024 — McMap. All rights reserved.