0

カスタム コンテンツ プロバイダの 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 を使用してテストを実行しています。

4

1 に答える 1

0

したがって、問題に対する答えは getProvider(); を呼び出すことでした。方法。これにより、実行スレッド内で AddressContentProvider にアクセスできるようになります。

    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);
}
于 2012-06-23T03:39:03.487 に答える