2017-08-03 107 views
2

所以我寫了我的應用程序,我使用KeyStore來加密/解密數據。 我也寫了一個不錯的Robolectric測試,但是當我試圖運行測試,我得到了以下異常:Robolectric給出java.security.KeyStoreException:找不到AndroidKeyStore

java.security.KeyStoreException: AndroidKeyStore not found 

以下代碼:

keyStoreInstance = KeyStore.getInstance("AndroidKeyStore"); 

這個任何解決方案?

所有我發現是這樣的: https://github.com/robolectric/robolectric/issues/1518

回答

2

那麼,這背後的原因是AndroidKeyStore實現是不存在的,當你通過Robolectric運行。

isTestRunner(){ 
    return Build.FINGERPRINT.equalsIgnoreCase("robolectric"); 
} 

如果你有一個類可以說YourKeystore.class其注入到其他許多類你仍然可以模擬出你YourKeystore.class: Using the Android KeyStore in Robolectric tests

您可以通過以下避免此異常,這使你能夠測試其他類。 如果你想測試YourKeystore.class本身,恐怕這是不可能的(見上面的鏈接)。