Android How to mock Application?

Viewed 627

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:

  1. Is there any way to easy mocking Application or stub static method without Powermock?
  2. Is there any working sample project with such stuff that is up-to-date for current Mockito, Powermock and JUnit4 versions?

Thank you!

0 Answers
Related