我有下面的方法,我需要为它编写单元测试。但我不能窥探类密钥库。它抛出以下异常。
org.mockito.exceptions.base.MockitoException: Unable to create mock instance of type 'KeyStore'
when(keyStoreMock.getCertificate(anyString())).thenReturn(certificateMock);
java.security.KeyStoreException: Uninitialized keystore
doNothing().when(keyStoreMock).load(any(InputStream.class),Mockito.any(char[].class));
java.lang.NullPointerException
public boolean verifySignature(String filePath, String extractContentsPath, String csvParams)
throws ServiceSDKException {
boolean result = false;
String typeOfCertificateStore = "";
String certificateStoreProvider = "";
String certificateName = "";
SignerInformationVerifier verifier = null;
if (filePath != null && extractContentsPath != null && csvParams != null && !filePath.isEmpty()
&& !extractContentsPath.isEmpty() && !csvParams.isEmpty()) {
try {
String[] receivedParams = csvParams.split(",");
typeOfCertificateStore = receivedParams[0];
certificateStoreProvider = receivedParams[1];
certificateName = receivedParams[2];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ServiceSDKException("csvParams should have type of certificate store, certificate store provider and certificate name respectively", e);
}
try {
Path signedDataFilePath = Paths.get(filePath);
Path pathToExtractContents = Paths.get(extractContentsPath);
KeyStore msCertStore = KeyStore.getInstance(typeOfCertificateStore, certificateStoreProvider);
msCertStore.load(null, null);
try {
verifier = new JcaSimpleSignerInfoVerifierBuilder()
.setProvider(certificateStoreProvider)
.build(((X509Certificate) msCertStore.getCertificate(certificateName)));
} catch (Exception e) {
throw new ServiceSDKException("Exception occurred when building certificate",e);
}
verify(signedDataFilePath, pathToExtractContents, verifier);
result = true;
} catch (KeyStoreException | NoSuchProviderException | IOException | NoSuchAlgorithmException
| CertificateException e) {
result = false;
throw new ServiceSDKException("Exception occurred while preparing to verify signature " , e);
}
} else {
throw new ServiceSDKException("FilePath,extract contents path or csv params cannot be empty or null");
}
return result;
}
@PrepareForTest(KeyStore.class)
@Test
public void should_verify_signature_when_verifySignature_called_with_fileName_and_certificate_details_in_verifySignature_method() throws Exception {
PowerMockito.mockStatic(KeyStore.class);
KeyStore keyStoreMock = PowerMockito.mock(KeyStore.class);
PowerMockito.when(KeyStore.getInstance(anyString())).thenReturn(keyStoreMock);
Mockito.doNothing().when(keyStoreMock).load(any(InputStream.class), Mockito.any(char[].class));
Certificate certificateMock = Mockito.mock(Certificate.class);
when(keyStoreMock.getCertificate(anyString())).thenReturn(certificateMock);
PowerMockito.when(KeyStore.getInstance("Windows-MY", "MoonMSCAPI")).thenReturn(keyStoreMock);
boolean result = signatureUtil.verifySignature("src//test//java//Updates.zip.signed.pkcs7"
, "src//test//java//Updates-retrieved.zip", "Windows-MY,MoonMSCAPI,Software View Certificate Authority");
Assert.assertTrue(result);
}
您可以在不使用PowerMock的情况下进行模拟,但方法稍微复杂一些:
KeyStoreSpi keyStoreSpiMock = mock(KeyStoreSpi.class);
KeyStore keyStoreMock = new KeyStore(keyStoreSpiMock, null, "test"){ };
keyStoreMock.load(null); // this is important to put the internal state "initialized" to true
从现在起,您只需模拟keystorespimock
,例如:
when(keyStoreSpiMock.engineGetKey(any(), any())).thenReturn(mock(Key.class);
例如,我有处理程序: 但是当我试图模拟它时,它实际上调用。如何使用来模拟它的方法?
如何模拟返回已强制转换的模拟对象的方法。 试验方法。
我正在SpringMVC上使用Mockito进行JUnit测试。测试使用@InjectMock和@Mock with when(方法(…)。然后返回(X)。问题是如何在@Inject实例中@Mock方法? 我尝试过创建两个实例,例如@InjectMocks Foo foInstance和@Mock Foo foInstanceMock;我的思维方式是区分注入什么实例和嘲笑什么实例。我也尝试使用间谍
下面是抽象类的代码: 现在,我应该如何为编写单元测试,并且应该如何在中模拟会话? 我在Stackoverflow上尝试了不同的解决方案,但仍然无法模拟它并获得会话模拟。
上面还有第二个问题。当我在Expects块中定义mock类时(如上),似乎只调用了构造函数,而不是,因此没有正确初始化对象。我通过将它移到方法中并在那里实例化该类来解决这个问题。看起来是这样的: 因此,这似乎得到了要调用的正确构造函数,但似乎还在调用。有什么见解吗?
我试图模拟一个单例类(SessionDataManager),通过调用静态getInstance()方法获得一个实例,但所有尝试似乎都返回null。 我试过了