问题
I'm attempting to write a few testcases that work against the Android Keystore. However, when I write the following test case:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class FancyPantsUnitTest {
@Test
public void buildKey() {
keyPairGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
keyPairGenerator.initialize(4096);
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
}
}
This fails with the following exception:
org.junit.ComparisonFailure: expected:<null> but was:<java.security.KeyStoreException: AndroidKeyStore not found>
I'm targeting API Level 23 if that helps.
回答1:
There is already a discussion on this at https://github.com/robolectric/robolectric/issues/1518 .
In short:
From java.security.Security javadoc:
The default values of security properties are read from an implementation-specific location, which is typically the properties file lib/security/java.security in the Java installation directory.
… which we probably don't want to encourage people to monkey with.
Looks like this will need to be a method intercept rule...
The same happens when trying PowerMockito.
来源:https://stackoverflow.com/questions/38213748/using-the-android-keystore-in-robolectric-tests