カスタム コンテンツ プロバイダの update() メソッドをテストしようとしています。update() メソッド内の getContext() メソッド呼び出しが null であるため、null ポインター例外を受け取ります。この問題を軽減するために、実装を試みましたAndroidTestCase.getContext().getApplicationContext() が null を返すのはなぜですか? 無駄に。私のテストクラスは次のようになります。
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);
}
挿入メソッドは次のようになります。
@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;
}
Platform 10 で Android 2.3.3 を使用してテストを実行しています。