I write androidTest for class that using SharedPreferences. Constructor of the class contains this code:
public PreferencesReader() {
sharedPrefs = MyApplication.getContext().getSharedPreferences(name, mode);
}
I need to test the constructor, but I have an error Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference because of cannot mock static method getContext() and Application class.
I've read a lot of pages, tried Powermock, that compiles with errors and so on.
So, my questions are:
- Is there any way to easy mocking
Applicationor stub static method withoutPowermock? - Is there any working sample project with such stuff that is up-to-date for current
Mockito,PowermockandJUnit4versions?
Thank you!