当我从测试方法中调用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);
答案 0 :(得分:2)
DecryptDataResponse response = (DecryptDataResponse) HSMService.getInstance().processRequest(decrypt);
您可以通过 Java Singleton Pattern 访问HSMService
对象。这种单身基本上是全局变量,软件开发人员认为自80年代后期以来就是邪恶......
您最好注入 HSMService
对象,最好是构造函数参数或任何其他依赖注入技术。
在这种情况下,您可以使用模拟替换HSMService
对象,模拟又会在调用DecryptDataResponse
方法时返回processRequest
类的模拟。