I have a subclass of Application like so:
public class AppState extends Application {
private static AppState instance;
private static Context context;
public static void injectContextAndInstanceForTesting(Context context, AppState instance) {
AppState.instance = instance;
AppState.context = context;
}
}
I create some mocks with:
mockSharedPrefs = mock(SharedPreferences.class);
mockAppState = mock(AppState.class);
mockContext = mock(Context.class);
when(mMockContext.getSharedPreferences(anyString(), anyInt())).thenReturn(mockSharedPrefs);
AppState.injectContextAndInstanceForTesting(mMockContext, mockAppState);
At this point I run my test and get a NullPointerException when the code hits
SharedPreferences.edit().putString();
Test failed to run to completion.
Reason: 'Instrumentation run failed due to 'java.lang.NullPointerException''.
Check device logcat for details
Tests on Nexus5 - 4.4.4 failed: Instrumentation run failed due to 'java.lang.NullPointerException'
Does anyone have experience mocking out SharedPreferences and/or Application? I'm not sure which of these is throwing the exception, as the unit test is running in an automated test harness that doesn't have access to the logcat.
Aucun commentaire:
Enregistrer un commentaire