我只是做單元測試,我叫卡單元測試類的實例越來越空
public class Card{
private KeyStore kestore;
private Cipher cipher;
public Card(){
}
public void generateRandom(){
keyStore = KeyStore.getInstance("AndroidKeyStore");
keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
keyStore.load(null);
keyGenerator.init(new
KeyGenParameterSpec.Builder(KEY_NAME,
KeyProperties.PURPOSE_ENCRYPT |
KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_CBC)
.setUserAuthenticationRequired(true)
.setEncryptionPaddings(
KeyProperties.ENCRYPTION_PADDING_PKCS7)
.build());
keyGenerator.generateKey();
}
public void init(){
cipher = Cipher.getInstance(
KeyProperties.KEY_ALGORITHM_AES + "/"
+ KeyProperties.BLOCK_MODE_CBC + "/"
+ KeyProperties.ENCRYPTION_PADDING_PKCS7);
keyStore.load(null);
SecretKey key = (SecretKey) keyStore.getKey(KEY_NAME,
null);
cipher.init(Cipher.ENCRYPT_MODE, key);
}
}
類和下面是我的單元測試代碼。
public class cardTest extends AndroidTestCase{
Card card;
@Override
protected void setUp() throws Exception {
super.setUp();
card = new Card();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public class testgenerateRandom(){
card.generateRandom();
}
public class testinit(){
card.init();
}
}
所以在上述單元測試,
的testinit()將調用卡類的初始化。那裏的密鑰庫變爲空。我已經初始化了第一個測試用例generateRandom()中的keystore。
每當第一個測試用例(testgenerateRandom())完成時,卡實例就變爲null。使密鑰庫也變爲空
爲什麼卡實例變爲空?那可以幫助我嗎?
請發佈您的真實代碼或至少是實際編譯的內容。 – m0skit0
更新了它,你可以看到它... @ m0skit0 – Vji
@downvoter我可以得到投票的理由嗎? – Vji