0

I want to test Unit test for SharedPreference. I don't know how can i mock these values, I just started unit testing. Can anyone please give me solution of this method, how can i test this method.

public static void oneMethod(Context context) {
    SharedPreferences prefs = context.getSharedPreferences("Constant", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("Value", 3);
    editor.apply();
    Model.setValue(true);
}
Navjot.jassal
  • 749
  • 2
  • 10
  • 29
  • What methods are you trying to test? Is SharedPreferences a 3rd party library? One shouldn't test 3rd party code. –  Jan 28 '20 at 18:50
  • Actually i was testing Util classes and testing the logics, but in here, i don't know what i need to test here, – Navjot.jassal Jan 28 '20 at 18:53
  • Please search before asking such questions -> [https://stackoverflow.com/questions/9748417/providing-test-data-for-sharedpreferences-for-robolectric](https://stackoverflow.com/questions/9748417/providing-test-data-for-sharedpreferences-for-robolectric) – Rodrigo Queiroz Jan 28 '20 at 19:53

1 Answers1

0

I write a class for shared preference and you can use and test so easy

package ir.hfathi.students.helper;

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

import androidx.annotation.Nullable;

/**
 * Created by Hamid on 07/05/2017.
 */

public class AppPreferenceTools {
    private SharedPreferences mPreference;
    private Context mContext;
    Methods method;

    public static final String STRING_PREF_UNAVAILABLE = "string preference unavailable";

    public AppPreferenceTools(Context context, String preferenceName) {
        if (method == null) method = new Methods();
        this.mContext = context;
        this.mPreference = this.mContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE);
    }

    public SharedPreferences getPreference() {
        return mPreference;
    }

    public void savePreferences(String name, String value) {
        mPreference.edit()
                .putString(name, value)
                .apply();
    }

    public void savePreferences(String name, int value) {
        mPreference.edit()
                .putInt(name, value)
                .apply();
    }

    public void savePreferences(String name, Boolean value) {
        mPreference.edit()
                .putBoolean(name, value)
                .apply();
    }

    public void savePreferences(String name, int value, String PreferencesName) {
        this.mPreference = this.mContext.getSharedPreferences(PreferencesName, Context.MODE_PRIVATE);
        mPreference.edit()
                .putInt(name, value)
                .apply();
    }


    public String getName(String name, @Nullable String defaultSTR) {
        return mPreference.getString(name, defaultSTR);
    }

    public Boolean getName(String name, Boolean defaultBoolean) {
        return mPreference.getBoolean(name, defaultBoolean);
    }

    public String getName(String name, @Nullable String defaultSTR, String Preferences) {
        this.mPreference = this.mContext.getSharedPreferences(Preferences, Context.MODE_PRIVATE);
        return mPreference.getString(name, defaultSTR);
    }

    public int getName(String name, @Nullable int defaultSTR) {
        return mPreference.getInt(name, defaultSTR);
    }

    public int getName(String name, @Nullable int defaultSTR, String Preferences) {
        this.mPreference = this.mContext.getSharedPreferences(Preferences, Context.MODE_PRIVATE);
        return mPreference.getInt(name, defaultSTR);
    }

    public void removeAllPrefs() {
        mPreference.edit().clear().apply();
    }

    public void removeItemPrefs(String removeItem) {
        try {
            mPreference.edit().remove(removeItem).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public int get_count_Preference() {
        return mPreference.getAll().size();
    }

}

and I give a sample how to use them

        public AppPreferenceTools getAppPreferenceTools(Context context, String PreferenceName) {
            return new AppPreferenceTools(context, PreferenceName);
        }

getAppPreferenceTools(this, AppConfig.HELP_DASHBOARD)
    .getName(AppConfig.HELP_DASHBOARD+header, "non")

getAppPreferenceTools(this, AppConfig.HELP_DASHBOARD)
    .savePreferences(AppConfig.HELP_DASHBOARD+header, "show");
hamid
  • 171
  • 1
  • 1
  • 9