gpt4 book ai didi

java - 如何在方法中模拟对象

转载 作者:行者123 更新时间:2023-11-30 02:31:11 27 4
gpt4 key购买 nike

当我从测试方法中调用 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);

最佳答案

DecryptDataResponse response = (DecryptDataResponse) HSMService.getInstance().processRequest(decrypt);

您可以通过Java 单例模式访问HSMService 对象。这种单例基本上是全局变量,自 80 年代末以来软件开发人员认为它们是邪恶的......

您最好注入(inject)HSMService对象,最好作为构造函数参数或任何其他依赖注入(inject)技术。在这种情况下,您可以使用模拟替换 HSMService 对象,该模拟又会在调用 processRequest 方法时返回 DecryptDataResponse 类的模拟。

关于java - 如何在方法中模拟对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44242959/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com