InstantiationException

InstantiationException

本文介绍了Robolectric的ContentProvider测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,每一个现在,然后有一个关于使用Robolectric测试自定义ContentProviders问题。然而,从未有过关于如何正确地做了具体而明确的答案。在2种不同的方法我已经迷迷糊糊:

I have noticed that every now and then there is a question about using Robolectric for testing custom ContentProviders. However, there has never been a concrete and unambiguous answer on how to do it properly. I have stumbled upon 2 different approaches:

  • 有一句话说,你可以简单地实例化一个内存中的ContentProvider,您可以使用插入和查询数据(https://gist.github.com/anonymous/6139359)

另外说使用ShadowContentResolver设置模拟指针数据(https://groups.google.com/d/msg/robolectric/r35mMirIkTs/xJJBNXl_RgwJ)

the other saying to use the ShadowContentResolver to set mock cursor data (https://groups.google.com/d/msg/robolectric/r35mMirIkTs/xJJBNXl_RgwJ)

不过,我越来越有两种方法一java.lang.InstantiationException。已经有一些SO帖子说,这是由于SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java)不被重写的Robolectric(Android + Robolectric - 的RuntimeException / InstantiationException在queryBuilder.query()中的ContentProvider )。

However, I'm getting a java.lang.InstantiationException with both approaches. There have been some SO posts saying that this is due to SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java) not being overriden in Robolectric (Android + Robolectric - RuntimeException / InstantiationException in queryBuilder.query() in ContentProvider).

我想我的问题是 - 是否有任何preffered的解决方法,使测试ContentProviders可能。还是有什么其他的办法,是更好然后这2上面提到的。

I guess my question is - are there any preffered workarounds that make testing ContentProviders possible. Or are there any other approaches that are better then those 2 mentioned above.

推荐答案

这是Robolectric测试正常工作对我来说:

This is the Robolectric test that worked fine for me:

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;

import org.joda.time.LocalDate;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowContentResolver;

import co.tomup.app.db.DbSchema;
import co.tomup.app.db.TomupContentProvider;
import co.tomup.app.db.model.CalendarDay;
import co.tomup.app.db.tables.CalendarDayTable;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class CalendarDayProviderTest {

    private ContentResolver mContentResolver;
    private ShadowContentResolver mShadowContentResolver;
    private TomupContentProvider mProvider;

    @Before
    public void setup() {
        mProvider = new TomupContentProvider();
        mContentResolver = Robolectric.application.getContentResolver();
        mShadowContentResolver = Robolectric.shadowOf(mContentResolver);
        mProvider.onCreate();
        ShadowContentResolver.registerProvider(DbSchema.AUTHORITY, mProvider);
    }

    @Test
    public void testInsertAndDelete() {
        // insert
        CalendarDay calendarDay = new CalendarDay();
        calendarDay.setId(1L);
        calendarDay.setDay(new LocalDate());
        calendarDay.setMoonPhase("new");
        calendarDay.setSunrise(1);
        calendarDay.setSunset(100);
        Uri insertionId = mContentResolver.insert(CalendarDayTable.CONTENT_URI,
                calendarDay.toSQLiteContentValues());
        Cursor cursorCheck = mShadowContentResolver.query(CalendarDayTable.CONTENT_URI,
                null, null, null, null);
        while (cursorCheck.moveToNext()) {
            CalendarDay calendarDayCheck = CalendarDay.fromSQLiteCursor(cursorCheck);
            assertEquals(calendarDay, calendarDayCheck);
        }
        assertTrue(cursorCheck.getCount() > 0);
        // delete
        mShadowContentResolver.delete(insertionId,
                null, null);
        cursorCheck = mShadowContentResolver.query(CalendarDayTable.CONTENT_URI,
                null, null, null, null);
        assertTrue(cursorCheck.getCount() == 0);
    }
}

这篇关于Robolectric的ContentProvider测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 16:53