即使添加了包,我仍然会变为null。我查看了源代码,结果发现StubPackageManager总是为此返回null,并且无法覆盖整个PackageManager类。
答案 0 :(得分:1)
在设置中:
MockPackageManager mockPackageManager = new MockPackageManager(
Robolectric.getShadowsAdapter());
RuntimeEnvironment.setRobolectricPackageManager(mockPackageManager);
子类:
class MockPackageManager extends DefaultPackageManager {
public MockPackageManager(ShadowsAdapter shadowsAdapter) {
super(shadowsAdapter);
}
@Override
public String getNameForUid(int uid) {
switch (uid) {
case UID_A:
return NAME_A;
case UID_B:
return NAME_B;
default:
return null;
}
}
@Override
public boolean isPermissionRevokedByPolicy(String s, String s1) {
return false;
}
}