I am trying to test my DB using ProviderTestCase2<T>
. I can see the test DB being created. As such I suppose, the tested content provider should use the test DB. But as soon as I try any calls against the MockContentResolver
(or the one created with newResolverWithContentProviderFromSql
), I get an UnsupportedOperationException
. This is documented for the MockContentResolver as normal behavior. As such I am a bit unsure on the purpose of the ProviderTestCase2.
How do you test your content providers?
Thanks
Android - Content Providers. A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class.
Create: Operation to create data in a content provider. Read: Used to fetch data from a content provider. Update: To modify existing data. Delete: To remove existing data from the storage. UI components of android applications like Activity and Fragments use an object CursorLoader to send query requests to ContentResolver.
A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network. sometimes it is required to share data across applications.
Create a new java file called StudentsProvider.java under the package com.example.MyApplication to define your actual provider and associated methods. Modify the default content of res/layout/activity_main.xml file to include a small GUI to add students records. No need to change string.xml.Android studio take care of string.xml file.
As far as I found, setting up the mock content resolver is not explicitly necessary - I might oversee cases where it is(maybe correct resolving of the provider via URI, hings that need corect getType() working), but for me, it was enough to do something like this:
package org.droidcon.apps.template.provider.test;
import org.droidcon.apps.template.provider.ProfileContract;
import org.droidcon.apps.template.provider.ProfileProvider;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.ProviderTestCase2;
public class ProfileProviderTest extends ProviderTestCase2<ProfileProvider> {
public ProfileProviderTest() {
super(ProfileProvider.class, ProfileProvider.class.getName());
}
protected void setUp() throws Exception {
super.setUp();
}
/**
* Very basic query test.
*
* Prerequisites:
* <ul>
* <li>A provider set up by the test framework
* </ul>
*
* Expectations:
* <ul>
* <li> a simple query without any parameters, before any inserts returns a
* non-null cursor
* <li> a wrong uri results in {@link IllegalArgumentException}
* </ul>
*/
public void testQuery(){
ContentProvider provider = getProvider();
Uri uri = ProfileContract.CONTENT_URI;
Cursor cursor = provider.query(uri, null, null, null, null);
assertNotNull(cursor);
cursor = null;
try {
cursor = provider.query(Uri.parse("definitelywrong"), null, null, null, null);
// we're wrong if we get until here!
fail();
} catch (IllegalArgumentException e) {
assertTrue(true);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With