Mockito For Testing SharedPreferences Helper Utils
<p><br>
I am getting a null pointer exception at <code>sharedPreferences.edit();</code> while using Mockito.</p>

<p>Here is my test method:</p>

<pre><code>Mockito.when(sharedPreference.edit()).thenReturn(editor);
            Mockito.when(editor.putString(Mockito.anyString(), Mockito.anyString())).thenReturn(editor);

            SharedPrefsUtils.setSharePreferenceString(context, "PREF_ID", "PREF_STRING");
            Assert.assertEquals("PREF_STRING", SharedPrefsUtils.getSharePreferenceString(context, "PREF_ID"));
</code></pre>

<p>&amp; the shared preferences the setSharePreferenceString looks like:</p>

<pre><code>SharedPreferences pref = context.getSharedPreferences(GENERIC_PREFERENCES_TAG,Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();

        editor.putString(id, value);
        editor.commit();
</code></pre>

<p>Can anyone tell me what is missing?</p>