vendredi 4 septembre 2015

Testcase for Android SQLite

Question on writing TestCase for SQLite.

I tried following to write a test, but it breaks down.

The Android Studio showed error java.lang.UnsupportedOperationException on the two error lines in the following code. Maybe someone can show me where it goes wrong.

public DeviceDatabase(Context context) {
    dbHelper = new DeviceSQLiteHelper(context);
    db = dbHelper.getWritableDatabase();        // error
}

The Test class with a MockContext. Honestly I don't know it can be used this way or not.

public class DeviceListControlTest {
    String bt_addr = "00:01:02:03:03:08";

    public DeviceDatabase mDeviceDatabase;

    public MockContext mMockContext;

    @Before
    public void setUp() throws Exception {
        // need mock context
        mMockContext = new MockContext();
        mDeviceDatabase = new DeviceDatabase(mMockContext);   // error
    }

    @Test
    public void testCreateDevice() throws Exception {
        mDeviceDatabase.createDevice("Hamburg", bt_addr, "HamburgBT");
        List<DeviceName> deviceNameList;
        deviceNameList = mDeviceDatabase.getDevices();

        boolean result = checkItem(deviceNameList, bt_addr);
        assertThat(true, is(equalTo(result)));
    }

    ....

Aucun commentaire:

Enregistrer un commentaire