Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing a custom ContentProvider in Android

I have written my content provider supposed to wrap access to 2 tables in a SqlLite database. Now I'd like to write some test cases for it but I have never done it. After reading the section on the developer guide, I must say that I did not manage to get anything tested.

Below is my code so far. This is the only class in the test project that corresponds to my main project. When I execute it in Eclipse, the emulator starts correctly, the packages get installed but it does not run the test:

Test run failed: Test run incomplete. Expected 1 tests, received 0

Here is the test class:

public class ArticleProviderTest extends ProviderTestCase2<ArticleProvider> {

    static final Uri[] validUris = new Uri[] { Articles.CONTENT_URI, 
       Pictures.CONTENT_URI,
       Pictures.getContentUriForArticleId(1) };

    public ArticleProviderTest(Class<ArticleProvider> providerClass, String providerAuthority) {
        super(providerClass, providerAuthority);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
    }

    public void testQuery() {
        ContentProvider provider = getProvider();    
        for (Uri uri : validUris) {
            Cursor cursor = provider.query(uri, null, null, null, null);
            assertNotNull(cursor);
        }    
    }
}

And the manifest file, if it helps:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="fr.marvinlabs.xxxx"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />
    <instrumentation android:targetPackage="fr.marvinlabs.xxxx" android:name="android.test.InstrumentationTestRunner" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />
    </application>
</manifest>

When I launch in debug configuration, breakpoints in the constructor and in the setUp don't get triggered. ?!

I also did not find much info on the net. Could anybody help me get some understanding on how the testing should be setup (basically create a test database file, fill it with some data, query it, ...)?

like image 290
Vincent Mimoun-Prat Avatar asked Apr 29 '11 23:04

Vincent Mimoun-Prat


3 Answers

Ok, got it. Mistake was that I was not providing the default constructor for the test class. I had overridden the wrong constructor:

public ArticleProviderTest(Class<ArticleProvider> providerClass, String providerAuthority) {
    super(providerClass, providerAuthority);
}

is now

public ArticleProviderTest() {
    super(ArticleProvider.class, "com.blah.azerty");
}

2am is the time when you cannot read the docs entirely well, afternoon is better :)

like image 61
Vincent Mimoun-Prat Avatar answered Oct 14 '22 00:10

Vincent Mimoun-Prat


I found NotePadProviderTest.java in the NotePad sample project provided by the SDK to be a good start.

like image 23
Bernd S Avatar answered Oct 14 '22 01:10

Bernd S


You should implement setUp() and tearDown() methods in which you create and delete the database.

This is a great example: http://www.google.com/codesearch/p?hl=en#IrmxZtZAa8k/tests/src/com/android/providers/calendar/CalendarProvider2Test.java

like image 27
Peter Knego Avatar answered Oct 14 '22 01:10

Peter Knego