Can't get ApplicationContext in Espresso tests

13,340

Solution 1

If you just want to use getSharedPreferences,

try

Activity activity = mActivityRule.getActivity();
SharedPreferences prefs = activity.getSharedPreferences("current.user", Context.MODE_PRIVATE);
assertTrue(prefs.getBoolean("IsLoggedIn", false));

Solution 2

Did you try this?

InstrumentationRegistry.getInstrumentation().getTargetContext()

Solution 3

You can use the following code:

ApplicationProvider.getApplicationContext()
Share:
13,340
Nxt3
Author by

Nxt3

Updated on June 06, 2022

Comments

  • Nxt3
    Nxt3 about 2 years

    I keep getting the following error when running my Espresso tests:

    Attempt to invoke virtual method 'android.content.Context android.app.Instrumentation.getTargetContext()' on a null object reference

    @RunWith(AndroidJUnit4.class)
    public class TestLogin extends AndroidTestCase {
    
        @Rule
        public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
    
        @Test
        public void testVerifyCredentials() {
            Context context = this.getContext().getApplicationContext();
            SharedPreferences prefs = context.getSharedPreferences("current.user", Context.MODE_PRIVATE);
            assertTrue(prefs.getBoolean("IsLoggedIn", false));
        }
    }
    

    I already tried this as an InstrumentationTestCase, and then tried instead to do Context context = this.getInstrumentation().getTargetContext().getApplicationContext(); but that still lead to the NPE.

    What am I doing wrong?

    The SessionManager class I'm using in my application to manage SharedPrefs:

    package com.nullpointexecutioners.buzzfilms.helpers;

    import android.content.Context; import android.content.SharedPreferences;

    import com.firebase.client.Firebase; import com.nullpointexecutioners.buzzfilms.Major;

    /**
     * Helper class for managing a session (i.e. persistence across launches of the app
     */
    public class SessionManager {
    
        private static SessionManager sInstance;
    
        private final SharedPreferences pref;
        private final SharedPreferences.Editor editor;
    
        //SharedPref file name
        private static final String PREF_NAME = "current.user";
    
        //All Shared Preferences Keys
        private static final String IS_LOGIN = "IsLoggedIn";
    
        //Username
        public static final String KEY_USERNAME = "username";
    
        //Name
        public static final String KEY_NAME = "name";
    
        //Email
        public static final String KEY_EMAIL = "email";
    
        //Major
        public static final String KEY_MAJOR = "major";
    
        //is an Admin
        public static final String IS_ADMIN = "isAdmin";
    
        final Firebase mRef = new Firebase("redacted");
    
        /**
         * Constructor for instance of SessionManager
         * @param context of session
         * @return instance of SessionManager
         */
        public static SessionManager getInstance(Context context) {
            if (sInstance == null) {
                sInstance = new SessionManager(context);
            }
            return sInstance;
        }
    
        /**
         * Constructor for SessionManager class
         * @param context of session
         */
        private SessionManager(Context context) {
            pref = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
            editor = pref.edit();
        }
    
        /**
         * Create login session
         * @param username to store in SharedPrefs
         * @param name name of a user
         * @param email to store in SharedPrefs
         * @param major major of a user
         * @param isAdmin determines if a user is admin or not
         */
        public void createLoginSession(String username, String name, String email, String major, boolean isAdmin) {
            /*Store each value into SharedPrefs*/
            editor.putBoolean(IS_LOGIN, true);
            editor.putString(KEY_USERNAME, username);
            editor.putString(KEY_NAME, name);
            editor.putString(KEY_EMAIL, email);
            if (major.equals(Major.NONE.toString())) {
                editor.putString(KEY_MAJOR, "NONE");
            } else {
                editor.putString(KEY_MAJOR, major);
            }
    
            editor.putBoolean(IS_ADMIN, isAdmin);
    
            //Commit changes to SharedPrefs
            editor.apply();
        }
    
        /**
         * Update the current Session's values
         * @param name to update
         * @param email to update
         * @param major to update
         */
        public void updateSession(String name, String email, String major) {
            /*Store the updated values into SharedPrefs*/
            editor.putString(KEY_NAME, name);
            editor.putString(KEY_EMAIL, email);
            if (major.equals(Major.NONE.toString())) {
                editor.putString(KEY_MAJOR, "NONE");
            } else {
                editor.putString(KEY_MAJOR, major);
            }
    
            //Commit changes to SharedPrefs
            editor.apply();
        }
    
        /**
         * Checks if current user is logged in
         * If false, the user is redirected to WelcomeActivity to login or register
         * @return true or false depending if we're logged in
         */
        public boolean checkLogin() {
            return pref.getBoolean(IS_LOGIN, false);
        }
    
        /**
         * Checks if current user is an Admin
         * If false, the user won't have access to Admin functions
         * @return true or false depending if the user is an Admin
         */
        public boolean checkAdmin() {
            return pref.getBoolean(IS_ADMIN, false);
        }
    
        /**
         * Getter for currently logged in user's username
         * @return current user's username
         */
        public String getLoggedInUsername() {
            String username = null;
            if (pref.contains(KEY_USERNAME) && pref.getBoolean(IS_LOGIN, false)) {
                username = pref.getString(KEY_USERNAME, null);
            }
            return username;
        }
    
        /**
         * Getter for currently logged in user's name
         * @return current user's name
         */
        public String getLoggedInName() {
            String name = null;
            if (pref.contains(KEY_NAME) && pref.getBoolean(IS_LOGIN, false)) {
                name = pref.getString(KEY_NAME, "name");
            }
            return name;
        }
    
        /**
         * Getter for currently logged in user's email
         * @return current user's email
         */
        public String getLoggedInEmail() {
            String email = null;
            if (pref.contains(KEY_EMAIL) && pref.getBoolean(IS_LOGIN, false)) {
                email = pref.getString(KEY_EMAIL, null);
            }
            return email;
        }
    
        /**
         * Getter for currently logged in user's major
         * @return current user's major
         */
        public String getLoggedInMajor() {
            String major = null;
            if (pref.contains(KEY_MAJOR) && pref.getBoolean(IS_LOGIN, false)) {
                major = pref.getString(KEY_MAJOR, null);
            }
            return major;
        }
    
        /**
         * Clears session credentials
         */
        public void logoutUser() {
            //UnAuth from Firebase
            mRef.unauth();
    
            //Clear SharedPrefs and set IS_LOGIN to false
            editor.clear();
            editor.putBoolean(IS_LOGIN, false);
            editor.commit();
        }
    
        /**
         * Getter for accessing the current SharedPrefs
         * @return this session's SharedPrefs
         */
        public SharedPreferences getPref() {
            return pref;
        }
    }
    
  • Nxt3
    Nxt3 about 8 years
    MainActivity.getApplicationContext() isn't possible inside this test. I can't reference getSharedPreferences() has a non-static method. As for my actual application, I have a SessionManager class that handles all interactions with SharedPreferences; should I use that instead? I've updated the OP with that code.
  • Siamaster
    Siamaster over 6 years
    Should have been accepted answer. getActivity() will try to launch the activity if it hasn't been created. I didn't want that.
  • Siamaster
    Siamaster over 6 years
    If he just want to use SharedPrefs, this code will try to launch the activity. It's better to use InstrumentationRegistry.getInstrumentation().getTargetContex‌​t().
  • Shawn
    Shawn over 4 years
    100% agree that this is the best answer.