But when we try to produce functional or integration tests of your Activities against your ContentProvider it's not so evident what test case to use. The most obvious choice is ActivityInstrumentationTestCase2 mainly if your functional tests simulate user experience because you may need sendKeys() or similar methods, which are readily available on these tests.

The first problem you may encounter is that's no clear where to inject a MockContentResolver in your test to be able to use a test database instance or database fixture with your ContentProvider. There's no way to inject a MockContext either.

We have visited Mock Objects before and evaluated Test Driven Development. You may want to take a look if you are not familiar with mock objects on Android.

You should extend this class to create your tests and everything will be setup automatically.

During the setup process the abstract method createDatabaseFixture(Context mockContext) is invoked to give you the opportunity of creating your database fixture.

The last step of the setup process invokes your Activity's onCreate() method.

MockContextWithMockContentResolver<P extends ContentProvider>

A mock context, with a mock content resolver using the specified ConentProvider under test. The mock context is a RenamingDelegatingContext that renames file and database operations prefixing them with "test." by default.

GenericMockContentResolver<P extends ConentProvider>

A generic mock content resolver using the specified ConentProvider under test. This content resolver uses the specified content provider to resolve data for authority.

examples

To demonstrate the use of this library we have an Activity that presents some data using a ContentProvider. They are very simple an similar to other examples with the same functionality. In this case we are using a content provider with authority on a simple table Samples.AUTHORITY.

Our Activity in the following example is MyMockContentResolverActivity and the content provider is MyContentProvider.

We will concentrate now on the tests.

content provider unit tests

This tests will be as usual implemented using ProviderTestCase2<P>. This is pretty standard so we are not pay much attention to it.

activity and content provider functional tests

We reach the point. MyMockContentResolverActivityTests class implements the tests extending ActivityAndContentProviderInstrumentationTestCase2 and specifying the Activity and ContentProvider under test as well as the authority, Samples.AUTHORITY in this particular example.

These tests can be run completely isolated from real data, so you can be confident and exercise your database through your Activity not worrying about your real data.