【发布时间】:2020-10-26 12:01:23
【问题描述】:
我正在为我的一个应用程序编写 JUnit 测试用例。我是 JUnit 框架的新手。我有多个 If 条件,null 条件,请查看测试用例方法并建议以下错误。请建议我们如何为 If 条件、null 条件和环境变量编写测试用例。
我遇到以下错误:
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
【问题讨论】:
标签: java spring spring-boot junit