1
我想在我的测试方法中调用getHSMDecryptedData
方法时在此处模拟响应对象。如何在方法中模拟对象
private String getHSMDecryptedData(String keysetName, int groupIndex,
String ksn, String encryptedData) {
String decryptedData = null;
try {
DecryptData decrypt = new DecryptData();
decrypt.setKeySet(keysetName);
decrypt.setKsnDescriptor("906");
decrypt.setKsn(ksn);
decrypt.setKeyType(HSMKeyTypeDataModel.TYPE_BDK);
decrypt.setEncryptionMode(HSMEncryptionMode.CBC);
decrypt.setInputFormat(HSMDataFormat.HEX_ENCODED_BINARY);
decrypt.setOutputFormat(HSMDataFormat.HEX_ENCODED_BINARY);
decrypt.setMessage(encryptedData);
// sending M2 command to HSM for decryption of encrypted data coming from CP
DecryptDataResponse response = (DecryptDataResponse) HSMService.getInstance().processRequest(decrypt);
System.out.println(response+"***************reponse");
if (response != null && response.getResponseCode() == HSMResponseCodes.APPROVED) {
decryptedData = response.getDecryptedMessage();
TraceLog.Info(getClass(),
"Message decrypted[" + decryptedData + "], original input[" + encryptedData + "], replacing original encrypted data!");
if (decryptedData == null) {
// throw new FirstadatException("Unable to get the decrypted Data from HSM ");
}
}//FirstadatException
这是我的测试方法:
HsmDataDecrypt hsmDataDecrypt = new HsmDataDecrypt();
try {
DecryptDataResponse response=mock(DecryptDataResponse.class);
//response.
Method method = hsmDataDecrypt.getClass().getDeclaredMethod("getHSMDecryptedData", String.class,int.class,String.class,String.class);
您可能会发现PowerMock可以解决您的问题,因为它允许您模拟静态方法调用。 **不要!**这是一个糟糕的解决方案,因为你的代码不好,所以只有这个问题。听蒂莫西,依赖注入将很好地解决你的问题并清理你的代码,而PowerMock只会让你隐藏真正的问题。 –
yup谢谢...... –