Search code examples
javaandroidnullpointerexceptionandroid-contentprovider

Content Provider JUnit Test and null getContext()


I am attempting to test the update() method of my custom content provider. I receive a null pointer exception due to the getContext() method call within the update() method being null. To alleviate this issue I have attempted implementing Why does AndroidTestCase.getContext().getApplicationContext() return null? to no avail. My test class looks like the following:

 public class AddressContentProviderTest extends ProviderTestCase2<AddressContentProvider> {

    public AddressContentProviderTest() {
    super(AddressContentProvider.class, AddressContentProvider.class.getName());
}

protected void setUp() throws Exception {
    super.setUp();
}
    public void testInsert() {
         Uri CONTENT_URI = Uri.parse("content://" + AddressContentProvider.AUTHORITY + "/address");
         AddressContentProvider addressContentProvider = new AddressContentProvider();
         ContentValues initialValues = new ContentValues();
         boolean isException = false;
         Uri returnURI = null;
         initialValues.put("country", "test");
         initialValues.put("region", "test");
         initialValues.put("city", "test");
         initialValues.put("state", "FL");
         initialValues.put("zip", "90210");
         initialValues.put("province", "test");
         initialValues.put("geo_location_id", "");

          returnURI = addressContentProvider.insert(CONTENT_URI, initialValues);

     assertTrue(returnURI != null);

}

The insert method looks like the following:

    @Override
    public int update(Uri uri, ContentValues values, String where, String[]  whereArgs) {
           SQLiteDatabase db =dbHelper.getWritableDatabase();
           int count;

          switch (sUriMatcher.match(uri)) {
            case ADDRESS:
                count = db.update(ADDRESS_TABLE_NAME, values, where, whereArgs);
                break;
            default:
         throw new IllegalArgumentException("Unknown URI " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return count;
}

I'm running my tests using Android 2.3.3 on Platform 10.


Solution

  • So the answer to the problem was to call the getProvider(); method. This gives me access to the AddressContentProvider within the thread of execution;

        public void testInsert() {
            Uri CONTENT_URI = Uri.parse("content://" + AddressContentProvider.AUTHORITY + "/address");
            AddressContentProvider addressContentProvider = getProvider();
            ContentValues initialValues = new ContentValues();
        //Uri returnURI = null;
            initialValues.put("country", "USA");
            initialValues.put("region", "test");
            initialValues.put("city", "Jacksonville");
            initialValues.put("state", "FL");
            initialValues.put("zip", "32258");
            initialValues.put("province", "test");
            initialValues.put("geo_location_id", "");
            Uri returnURI = addressContentProvider.insert(CONTENT_URI, initialValues);
            assertTrue(returnURI != null);
    }