MatzunaTata
MatzunaTata

Reputation: 74

Android Content Provider Test the REAL content Provider

hope you can help me ...

tl:dr
How can i write JUnit Tests which will NOT use the classes IsolatedContext and MockContentResolver ?
I want to affect the REAL content Provider and not the Mock Database.

General
I have to write JUnit Tests for a special ContentProvider at work. This Content Provider is connected to some different Hardware and sets there some values.
I must check the Hardware Values AND the Values of the Content Provider Database.

Construct
-> ContentProvider -> Hardware Interface -> Hardware -> HardwareInterface-> ContentProvider

Code

public class DataLayerTests extends ProviderTestCase2<DataLayer> {

private static final String TAG = DataLayerTests.class.getSimpleName();

MockContentResolver mMockResolver;

public DataLayerTests() {
    super(DataLayer.class, Constants.DATA_LAYER_AUTHORITY);
}

@Override
protected void setUp() throws Exception {
    super.setUp();
    Log.d(TAG, "setUp: ");
    mMockResolver = getMockContentResolver();
}

@Override
protected void tearDown() throws Exception {
    super.tearDown();
    Log.d(TAG, "tearDown:");
}

public void testActiveUserInsert__inserts_a_valid_record() {
    Uri uri = mMockResolver.insert(ActiveUserContract.CONTENT_URI, getFullActiveUserContentValues());
    assertEquals(1L, ContentUris.parseId(uri));
}}


The real Database should be affected as well as the Real ContentRescolver should be used. How could i arcive this ?

Upvotes: 1

Views: 881

Answers (1)

XGouchet
XGouchet

Reputation: 10203

You can use Robolectric to test the real content provider, affecting a real sqlite database.

Robolectric is an implementation of the Android framework that can be run in any JVM, and thus can be used for tests.

Please note that the sqlite database will live in a temp folder on your computer and not on a phone or emulator.

If you want the tests to happen inside a real phone, you should look into Instrumented tests

Upvotes: 1

Related Questions