Created
December 9, 2011 09:36
-
-
Save avh4/1450898 to your computer and use it in GitHub Desktop.
how to make an ActivityUnitTestCase that depends on a ContentProvider use test databases
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void setUp() throws Exception { | |
super.setUp(); | |
// Create a Context for our content provider that will use the test.* | |
// database instead of the production database | |
final String filenamePrefix = "test."; | |
final RenamingDelegatingContext testDatabaseContext = new RenamingDelegatingContext( | |
getInstrumentation().getTargetContext(), getInstrumentation() | |
.getTargetContext(), filenamePrefix); | |
// Instantiate our content provider and make it use the Context created | |
// above | |
mProvider = new MyContentProvider(); | |
mProvider.attachInfo(testDatabaseContext, null); | |
final boolean onCreate = mProvider.onCreate(); | |
assertTrue("ContentProvider reported failure in onCreate()", onCreate); | |
// Create a mock ContentResolver that will give access to our content | |
// provider and nothing else | |
mResolver = new MockContentResolver(); | |
mResolver.addProvider(MyContentProvider.AUTHORITY, mProvider); | |
// Create a Context for our activity under test that will use our | |
// content resolver (and thus our content provider), and will otherwise | |
// behave as the normal Context for an ActivityUnitTestCase | |
mActivityContext = new ContextWrapper(getInstrumentation() | |
.getTargetContext()) { | |
@Override | |
public ContentResolver getContentResolver() { | |
return mResolver; | |
} | |
}; | |
setActivityContext(mActivityContext); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment