Testing custom ContentProvider on Android

I wrote that my content provider was supposed to wrap access to 2 tables in the SqlLite database. Now I would like to write some test cases, but I never did it. After reading the section in the developer's guide, I have to say that I was not able to verify anything.

Below is my code for now. This is the only class in the test project that matches my main project. When I run it in Eclipse, the emulator starts correctly, the packages are installed, but it does not run the test:

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

Here is a 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 that 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 run the debug configuration, breakpoints in the constructor and in setUp do not start.?!

I also did not find much information on the net. Can someone help me understand how to set up testing (basically create a test database file, fill it with some data, request it ...)?

+7
source share
3 answers

Ok, got it. The error was that I did not provide a default constructor for the test class. I redefined the wrong constructor:

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

Now

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

2am is the time when you cannot read the documents completely, but in the afternoon it’s better :)

+7
source

I found NotePadProviderTest.java in the NotePad example project provided by the SDK for a good start.

+3
source

You should implement the 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

+2
source

All Articles