单元测试 class 实例为空
Unit testing class instance getting null
我正在为我的 class 命名卡做单元测试
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() 将调用 Card 的初始化 class。那里的密钥库变为空。我已经在 generateRandom() 中初始化了密钥库,这是第一个测试用例。
每当第一个测试用例 (testgenerateRandom()) 完成时,卡片实例变为空。这样密钥库也变为空
为什么卡片实例变为空?有什么可以帮助我的吗?
请记住,JUnit 会为其运行的每个测试方法创建一个新的 cardTest
class 实例,这意味着 testgenerateRandom()
不会在 [=12= 之前被调用] 正如您可能期望的那样,这两种方法将使用 Card
的不同实例。确保在 setUp()
中为每个测试方法正确配置被测实例。
我正在为我的 class 命名卡做单元测试
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() 将调用 Card 的初始化 class。那里的密钥库变为空。我已经在 generateRandom() 中初始化了密钥库,这是第一个测试用例。
每当第一个测试用例 (testgenerateRandom()) 完成时,卡片实例变为空。这样密钥库也变为空
为什么卡片实例变为空?有什么可以帮助我的吗?
请记住,JUnit 会为其运行的每个测试方法创建一个新的 cardTest
class 实例,这意味着 testgenerateRandom()
不会在 [=12= 之前被调用] 正如您可能期望的那样,这两种方法将使用 Card
的不同实例。确保在 setUp()
中为每个测试方法正确配置被测实例。