我在我的项目中使用了Dagger2,KeyStoreKeyGenerator (来自in.co.ophio.secure),并且我想使用Robolectric来测试我的片段。
我将presenter注入到我的片段中。演示者注入了userPrefs。UserPrefs实现了KeyStoreKeyGenerator
class UserPreferences(val application: App) : UserPreferencesAPI {
// another methods and fields
private val keyGenerator = KeyStoreKeyGenerator.get(application, application.packageName)
}这是我的演示者
class MainPresenter(...,
val sharedPreference: UserPreferencesAPI)这是我的测试
private MainFragment fragment;
private MainActivity activity;
@Before
public void setUp() {
activity = Robolectric.buildActivity(MainActivity.class).create().start().resume().get();
fragment = MainFragment.Companion.newInstance();
}
@Test
public void shouldBeNotNull() {
Assertions.assertThat(activity).isNotNull();
}运行测试后,我看到:
java.lang.NullPointerException
at android.security.KeyStore.isHardwareBacked(KeyStore.java:318)
at android.security.KeyChain.isBoundKeyAlgorithm(KeyChain.java:397)
at in.co.ophio.secure.core.KeyStoreKeyGenerator.<init>(KeyStoreKeyGenerator.java:41)
at in.co.ophio.secure.core.KeyStoreKeyGenerator.get(KeyStoreKeyGenerator.java:56)
at unofficial.coderoid.wykop.newapp.utils.UserPreferences.<init>(UserPreferences.kt:24)我应该创建影子KeyStoreKeyGenerator吗?我应该用接口包装KeyStore类吗?
发布于 2017-02-21 20:58:56
我设法通过编写一个自定义的影子http://robolectric.org/custom-shadows/来解决它
@Implements(android.security.KeyChain.class)
public class KeyChainShadow {
@RealObject
private KeyChain keyChain;
@Implementation
public static boolean isBoundKeyAlgorithm(String algorithm) {
return false;
}
}别忘了用注解你的测试
@Config(shadows = KeyChainShadow.class)https://stackoverflow.com/questions/40192294
复制相似问题