java - 方法 thenReturn(Future) 在 OngoingStubbing 类型中> 不适用于参数(字符串)
问题描述
我正在为我的一个应用程序编写 JUnit 测试用例。我是 JUnit 框架的新手。我有多个 If 条件,null 条件,请查看测试用例方法并建议以下错误。请建议我们如何为 If 条件、空条件和环境变量编写测试用例。
我收到以下错误:
The method thenReturn(Future<String>) in the type OngoingStubbing<Future<String>> is not applicable for the arguments (String)
对于下面的行。
when(executorService.submit(new AuditExecutor(reqMap, endpoint_start, restTemplate))).thenReturn("Success");
请参阅下面的服务类方法。
public synchronized boolean preExecutionAudit(String auditpoint , Object obj) {
LOG.debug("Processing Auditing request");
Map<String, String> reqMap = new HashMap<String, String>();
try {
if (null == configMap) {
LOG.debug("Initiating request for AuditPoint Configuration");
//configMap = restTemplate.getForObject(endpoint_config, Map.class);
setConfig();
// For Backward Compatibility. To be refactored after all services standardize
// the environment variables
if (null != System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)) {
LOG.debug("Central Config taking precedence "
+ System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR));
endpoint_register = System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)
+ AuditConstants.REL_PATH_CONTEXT + AuditConstants.REL_PATH_REGISTER;
endpoint_registerauditpoint = System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)
+ AuditConstants.REL_PATH_CONTEXT + AuditConstants.REL_PATH_REGISTER_AP;
endpoint_start = System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)
+ AuditConstants.REL_PATH_CONTEXT + AuditConstants.REL_PATH_START;
endpoint_end = System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)
+ AuditConstants.REL_PATH_CONTEXT + AuditConstants.REL_PATH_END;
endpoint_config = System.getenv(AuditConstants.AUDIT_SERVICE_BASE_URL_ENV_VAR)
+ AuditConstants.REL_PATH_CONTEXT + AuditConstants.REL_PATH_CONFIG;
auditFilterInfo = new HashMap<>();
auditFilterInfo.put(AuditConstants.AUDIT_FILTER_FIELD_REQ_ID,
AuditConstants.AUDIT_FILTER_METHOD_REQ_ID);// This need to be generalized if demands
// more fields
} else {
LOG.debug("Relying on Local Config......");
}
} else {
LOG.info("AuditPoint Configuration Cached");
}
if (null != configMap && configMap.containsKey(auditpoint)) {
LOG.debug("Audit Point is Registered" + configMap);
if (null != obj) {
reqMap.put(auditpoint, new Gson().toJson(obj));
} else {
reqMap.put(auditpoint, null);
}
executorService.submit(new AuditExecutor(reqMap, endpoint_start, restTemplate));
} else {
LOG.debug("Audit Point is NOT Registered ");
reqMap.put(auditpoint, "NO Object Required");
executorService.submit(new AuditExecutor(reqMap, endpoint_registerauditpoint, restTemplate));
}
} catch (Exception e) {
LOG.error("Start Audit Failed and Failure Ignored " + auditpoint, e);
}
return true;
}
我已经为上述服务类方法编写了下面的测试用例方法。
@Test
void preExecutionAuditForIfCondition() {
// when(configMap).thenReturn(null);
when(auditLibService.setConfig()).thenReturn(configMap);
when(System.getenv(ArgumentMatchers.anyString())).thenReturn("Success");
Map<String, String> reqMap = new HashMap<String, String>();
String auditpoint = "";
String endpoint_start = "";
reqMap.put(auditpoint, null);
when(executorService.submit(new AuditExecutor(reqMap, endpoint_start, restTemplate))).thenReturn("Success");
Object obj = null;
boolean value = auditLibService.preExecutionAudit(auditpoint, obj);
assertEquals(true, value);
}
这里提交是一个内置函数。
submit() is a Inbuilt method. Please see below method definition.
<String> Future<String> java.util.concurrent.ExecutorService.submit(Callable<String> task)
submit
<T> Future<T> submit(Callable<T> task)
Submits a value-returning task for execution and returns aFuture representing the pending results of the task. TheFuture's get method will return the task's result uponsuccessful completion.
If you would like to immediately block waitingfor a task, you can use constructions of the form result = exec.submit(aCallable).get();
Note: The Executors class includes a set of methodsthat can convert some other common closure-like objects,for example, PrivilegedAction to Callable form so they can be submitted.
Type Parameters:T - the type of the task's resultParameters:task - the task to submitReturns:a Future representing pending completion of the taskThrows:RejectedExecutionException - if the task cannot bescheduled for executionNullPointerException - if the task is null
解决方案
executor.submit(...)
你可以通过做存根
Future mockFuture = mock(Future.class);
when(mockFuture.get()).thenReturn("Success");
when(executorService.submit(any(Callable.class))).thenReturn(mockFuture);
提交正在返回Future<T>
,而您仅通过String
推荐阅读
- c# - Array[] 中的 Sprite[] 不起作用
- python - 模块导入错误图
- python - 如果太长,通过 child_process 从 Python 向 Node 发送 JSON 会被截断,如何解决?
- javascript - WebAssembly 使用什么引擎?
- python - numpy:从(索引,值)字典创建数组
- javascript - 我使用 java Servlets 提交表单并以 JSON 格式发送数据后无法重定向到另一个页面
- python - 索引溢出 - Python
- c++ - 在 C++ 中获取 epoc 时间(隐式打印十六进制值)
- clips - 剪辑如何不重复执行规则?
- java - 外部对象引用/多对象提交