How to test content providers on Android
android, android-contentprovider, junit, sqlite
Solution
Extend ProviderTestCase2 to override getMockContentResolver() and return your own class derived from MockContentResolver.
public class MyProviderTestCase2 extends ProviderTestCase2 {
@Override
public MockContentResolver getMockContentResolver () {
return new MyMockContentResolver();
}
}
MyMockContentResolver will need to override any methods you want to test in your ContentProvider.
Then you should be able to run any tests you want on your content provider while it's isolated by ProviderTestCase2
Problem
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