我使用的是 Android Studio 1.2,Robolectric 3.0-rc2。
我有两个测试类,一个MotdTest
使用一种测试 POJO json 序列化和反序列化的测试方法调用。另一个叫做UserInfoTest
,它包含 4 个测试方法,用于测试我设置的用户信息SharedPreferences
。如果我UserInfoTest
单独运行,所有 4 种测试方法总是通过。但是,如果我运行所有测试,则测试MotdTest
成功,但两种方法UserInfoTest
总是失败。我现在通过调用从命令行运行./gradlew test
有谁知道为什么我运行所有测试时我的一些测试都失败了?在我的 UserInfoTest 中,我确实有一个带@After
注释的方法,我通过在SharedPreferences.Editor
.
用户信息测试:
testOnSignIn()
失败,assertThat(6, equalTo(prefs.getAll().size()));
因为 的大小prefs
为 0。
并且testIsSignedIn()
失败了assertThat(UserInfo.isSignedIn(), is(false));
@RunWith(MyRoboRunner.class)
@Config(constants = BuildConfig.class)
public class UserInfoTest {
private String mExpectedId;
private String mExpectedName;
private String mExpectedEmail;
private String mExpectedToken;
private String mExpectedKey;
@Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
mExpectedId = "someiD";
mExpectedName = "johnny boy";
mExpectedEmail = "some@email.com";
mExpectedToken = "Session Token";
mExpectedKey = "Session Key";
}
@After
public void tearDown() {
SharedPreferences prefs = RuntimeEnvironment.application.getSharedPreferences(
UserInfo.PREFERENCES, Context.MODE_PRIVATE);
prefs.edit().clear().commit();
mExpectedId = null;
mExpectedName = null;
mExpectedEmail = null;
mExpectedToken = null;
mExpectedKey = null;
}
@Test
public void testOnSignIn() {
SharedPreferences prefs = RuntimeEnvironment.application.getSharedPreferences(
UserInfo.PREFERENCES, Context.MODE_PRIVATE);
UserInfo.onSignIn(mExpectedId, mExpectedName, mExpectedEmail, mExpectedKey, mExpectedToken);
assertThat(mExpectedKey, equalTo(UserInfo.getSessionKey()));
assertThat(mExpectedToken, equalTo(UserInfo.getSessionToken()));
assertThat(mExpectedId, equalTo(UserInfo.getUserId()));
assertThat(mExpectedEmail, equalTo(UserInfo.getUserEmail()));
assertThat(mExpectedName, equalTo(UserInfo.getUserName()));
assertThat(6, equalTo(prefs.getAll().size()));
}
@Test
public void testOnSignOut() {
UserInfo.onSignIn(mExpectedId, mExpectedName, mExpectedEmail, mExpectedKey, mExpectedToken);
// Set Over21 to make sure we unset this value on Signout
UserInfo.setIsOver21(true);
UserInfo.onSignOut();
SharedPreferences prefs = RuntimeEnvironment.application.getSharedPreferences(
UserInfo.PREFERENCES, Context.MODE_PRIVATE);
assertThat(UserInfo.getSessionKey(), nullValue());
assertThat(UserInfo.getSessionToken(), nullValue());
assertThat(UserInfo.isOver21Set(), is(false));
assertThat(prefs.getAll().size(), equalTo(0));
}
@Test
public void testIsSignedIn() {
assertThat(UserInfo.isSignedIn(), is(false));
UserInfo.onSignIn(mExpectedId, mExpectedName, mExpectedEmail, mExpectedKey, mExpectedToken);
assertThat(UserInfo.isSignedIn(), is(true));
UserInfo.onSignOut();
assertThat(UserInfo.isSignedIn(), is(false));
}
@Test
public void testIsOver21Set() {
assertThat(UserInfo.isOver21Set(), is(false));
UserInfo.setIsOver21(false);
assertThat(UserInfo.isOver21Set(), is(true));
}
}
用户信息:
App
是一个Application
子类并且App.getInstance()
是一个单例。
private static final SharedPreferences PREFS = App.getInstance()
.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
public static void onSignIn(String userId, String fullName, String email, String sessionKey,
String sessionToken) {
SharedPreferences.Editor editor = PREFS.edit();
editor.putString(PROPERTY_USER_ID, userId);
editor.putString(PROPERTY_USER_NAME, fullName);
editor.putString(PROPERTY_USER_EMAIL, email);
editor.putString(PROPERTY_SESSION_KEY, sessionKey);
editor.putString(PROPERTY_SESSION_TOKEN, sessionToken);
editor.putStringSet(PROPERTY_PENDING_SCANS, new HashSet<String>());
editor.commit();
}
public static boolean isSignedIn() {
return getSessionToken() != null;
}
MyRoboRunner:这是一个修改版本,因为我在 mac 上,也因为我在我的项目中以 API 22 为目标,但 robolectric 还不支持,所以我针对 API 21 运行测试。
public class MyRoboRunner extends RobolectricGradleTestRunner {
public MyRoboRunner(Class<?> klass) throws InitializationError {
super(klass);
}
protected AndroidManifest getAppManifest(Config config) {
AndroidManifest appManifest = super.getAppManifest(config);
String moduleRoot = getModuleRootPath(config);
//can use this line instead dynamic path resolution when AS bug is fix, or use @Config
//FsFile androidManifestFile = appManifest.getAndroidManifestFile();
FsFile androidManifestFile = FileFsFile.from(moduleRoot,
appManifest.getAndroidManifestFile().getPath()
.replace("bundles", "manifests/full"));
FsFile resDirectory = FileFsFile.from(moduleRoot, appManifest.getResDirectory().getPath());
FsFile assetsDirectory = FileFsFile
.from(moduleRoot, appManifest.getAssetsDirectory().getPath());
return new AndroidManifest(androidManifestFile, resDirectory, assetsDirectory) {
@Override
public int getTargetSdkVersion() {
//lollipop bc it's highest that robolectric 3.0 supports
return Build.VERSION_CODES.LOLLIPOP;
}
@Override
public int getMinSdkVersion() {
return Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1;
}
};
}
private String getModuleRootPath(Config config) {
String moduleRoot = config.constants().getResource("").toString().replace("file:", "");
return moduleRoot.substring(0, moduleRoot.indexOf("/build"));
}
}