我想测试SharedPreference的单元测试。我不知道如何模拟这些值,我刚刚开始进行单元测试。任何人都可以给我这种方法的解决方案,我该如何测试这种方法。
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);
}
答案 0 :(得分:0)
我为共享首选项编写了一个类,您可以轻松使用和测试
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();
}
}
然后我给出一个示例,说明如何使用它们
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");