Android Applicationtestcase Using A Mockcontext
I'm new to Android testing and I'm trying to create an ApplicationTestCase using a MockContext (well actually I'm trying to use a Renaming Mock Context). But I keep getting an Asse
Solution 1:
As a workaround for that book sample, check the android developer guide to ApplicationTestCase: "If simply run your tests as-is, your Application will be injected with a fully-functional Context" (http://developer.android.com/reference/android/test/ApplicationTestCase.html).
A few lines of the Setup method must be commented to get the test working:
protectedvoidsetUp()throws Exception
{
super.setUp();
// final RenamingMockContext mockContext = new RenamingMockContext(// getContext());// setContext(mockContext);
createApplication();
mApplication = getApplication();
}
Solution 2:
I've used AndroidTestCase to mock a simple context.
classExampleTestextendsAndroidTestCasepublicvoidsetUp() {
Contextc=newDelegatedMockContext(getContext())
}
classDelegatedMockContextextendsMockContext {
private Context mDelegatedContext;
privatestaticfinalStringPREFIX="test.";
publicDelegatedMockContext(Context context) {
mDelegatedContext = context;
}
@Overridepublic String getPackageName(){
return PREFIX;
}
@Overridepublic SharedPreferences getSharedPreferences(String name, int mode) {
return mDelegatedContext.getSharedPreferences(name, mode);
}
}
}
Its just a bog standard Context, but will get you going
Post a Comment for "Android Applicationtestcase Using A Mockcontext"