我从谷歌的示例中获取灵感,了解如何通过创建SharedPreferences
类来测试您的SharedPreferencesHelper
代码:
你可以看到该类使用类中硬编码的实际字符串作为sharedPreferences
的键 - 这里是类的摘录:
public class SharedPreferencesHelper {
// Keys for saving values in SharedPreferences.
static final String KEY_NAME = "key_name";
static final String KEY_DOB = "key_dob_millis";
static final String KEY_EMAIL = "key_email";
public boolean savePersonalInfo(SharedPreferenceEntry sharedPreferenceEntry){
// Start a SharedPreferences transaction.
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(KEY_NAME, sharedPreferenceEntry.getName());
editor.putLong(KEY_DOB, sharedPreferenceEntry.getDateOfBirth().getTimeInMillis());
editor.putString(KEY_EMAIL, sharedPreferenceEntry.getEmail());
// Commit changes to SharedPreferences.
return editor.commit();
}
在此处使用SharedPreferencesHelperTest
类进行测试时,他们使用上述类中定义的相同变量访问模拟的sharedPreferences
:
该类的摘录显示如下:
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_NAME), anyString()))
.thenReturn(mSharedPreferenceEntry.getName());
when(mMockSharedPreferences.getString(eq(SharedPreferencesHelper.KEY_EMAIL), anyString()))
.thenReturn(mSharedPreferenceEntry.getEmail());
when(mMockSharedPreferences.getLong(eq(SharedPreferencesHelper.KEY_DOB), anyLong()))
.thenReturn(mSharedPreferenceEntry.getDateOfBirth().getTimeInMillis());
谷歌这样做的方式允许他们绕过使用context
从 string.xml 资源文件中提取字符串并在sharedPreferences
内查询字符串的问题就像通常假设使用getString()
一样,即:
mSharedPreferences.getString(context.getString(R.string.name),"");
但是,我已经读过不可能使用context.getString
因为它是最终方法而且mockito无法模拟最终方法:
Mockito - Overriding a method that takes primitive parameters
如何使用mockito对getString
的任何方法进行单元测试?使用getString
的任何方法都不起作用,我的单元测试将失败。
这是我为SharedPreferences
撰写的课程,我想使用sharedPreferences
编写的getStrings
密钥对其进行测试:
public class SharedPreferencesHelper {
// The injected SharedPreferences implementation to use for persistence.
private final SharedPreferences mSharedPreferences;
private Context context;
public SharedPreferencesHelper(SharedPreferences sharedPreferences, Context context) {
mSharedPreferences = sharedPreferences;
this.context = context;
}
public boolean saveName(String name) {
// Start a SharedPreferences transaction.
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(context.getString(R.string.name), name);
return editor.commit();
}
public String fetchName() {
// Start a SharedPreferences transaction.
return mSharedPreferences.getString(context.getString(R.string.name),"");
}
public boolean saveGender(String gender) {
SharedPreferences.Editor editor = mSharedPreferences.edit();
editor.putString(context.getString(R.string.gender), gender);
return editor.commit();
}
}
这是我为上述课程编写的测试 - 它与google的SharedPreferencesHelperTest非常相似:
@RunWith(MockitoJUnitRunner.class)
public class SharedPreferencesHelperTest {
private static final String TEST_NAME = "Test name";
private SharedPreferencesHelper mMockSharedPreferencesHelper;
@Mock
SharedPreferences mMockSharedPreferences;
@Mock
SharedPreferences.Editor mMockEditor;
@Mock
MockContext context;
@Before
public void setUp() throws Exception {
// Create a mocked SharedPreferences.
mMockSharedPreferencesHelper = createMockSharedPreference();
}
@Test
public void testSaveName() throws Exception {
boolean success = mMockSharedPreferencesHelper.saveName(TEST_NAME);
Timber.e("success " + success);
assertThat("Checking that name was saved... returns true = " + success,
success, is(true));
String name = mMockSharedPreferencesHelper.fetchName();
Timber.e("name " + name);
assertThat("Checking that name has been persisted and read correctly " + name,
TEST_NAME,
is(name));
}
/**
* Creates a mocked SharedPreferences.
*/
private SharedPreferencesHelper createMockSharedPreference() {
// Mocking reading the SharedPreferences as if mMockSharedPreferences was previously written
// correctly.
when(mMockSharedPreferences.getString(Matchers.eq("name"), anyString()))
.thenReturn(TEST_NAME);
when(mMockSharedPreferences.getString(Matchers.eq("gender"), anyString()))
.thenReturn("M");
// Mocking a successful commit.
when(mMockEditor.commit()).thenReturn(true);
// Return the MockEditor when requesting it.
when(mMockSharedPreferences.edit()).thenReturn(mMockEditor);
return new SharedPreferencesHelper(mMockSharedPreferences, context);
}
}
运行测试失败,因为我在getString
课程中使用了SharedPreferencesHelper
。如果我对密钥进行硬编码,我就不会收到错误,即:
public String fetchName() {
// Start a SharedPreferences transaction.
return mSharedPreferences.getString("name","");
}
不应该在代码中对字符串进行硬编码,那么如何解决这个难题呢?
答案 0 :(得分:5)
您可以通过不使用Context
获取字符串资源来解决此问题,但可以使用Resources
类。 Resources
具有相同的getString
方法,但不是最终方法。
https://developer.android.com/reference/android/content/res/Resources.html#getString(int)
或者你可以尝试使用Robolectric来解决这个问题,因为它们可以实现上下文。
答案 1 :(得分:2)
2017.06回答:
我遵循jbarat的建议使用Robolectric。这是一个有点陡峭的曲线,因为事情会被改变/弃用/删除/与每个版本的Robolectric / Android Studio不兼容。
使用Android Studio 2.3.3和Robolectric 3.3.2,我或多或少地做了一些工作:
getString
的所有方法都应接受Context
作为第一个参数,以使其可模仿Context
build.gradle
:
testCompile "org.robolectric:robolectric:3.3.2"
testCompile "org.robolectric:shadows-multidex:3.3.2"
MyClass.java
:
public static String myGetString(Context context) {
return context.getString(R.string.my_string);
}
测试:
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, packageName = "com.myapplication", sdk = 19)
public class MyTest {
private Context mockContext;
@Before
public void setUp() {
// inject context provided by Robolectric
mockContext = RuntimeEnvironment.application.getApplicationContext();
}
@Test
public void test_myGetString() {
assertEquals("MY STRING", MyClass.myGetString(mockContext));
}
}
答案 2 :(得分:0)
要启用最终方法的模拟,您需要Mockito v2并按照此处的说明进行操作 Mock the unmockable: opt-in mocking of final classes/methods
在
src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker
中创建一个包含一行的文件:mock-maker-inline
执行此操作后,您可以模拟getString()when(mMockContext.getString(R.string.string_name)).thenReturn("Mocked String");