【问题标题】:The method thenReturn(Future<String>) in the type OngoingStubbing<Future<String>> is not applicable for the arguments (String)OngoingStubbing<Future<String>> 类型中的 thenReturn(Future<String>) 方法不适用于参数 (String)
【发布时间】: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


    【解决方案1】:

    您可以通过以下方式存根executor.submit(...)

    Future mockFuture = mock(Future.class);
    when(mockFuture.get()).thenReturn("Success");
    when(executorService.submit(any(Callable.class))).thenReturn(mockFuture);
    

    提交返回Future&lt;T&gt;,而您只传递String

    【讨论】:

    • 嗨@convexmethod,感谢您的回答。我尝试使用 doNothing.when() 方法,但 doNothing 方法出错。您能否用一行代码示例进一步解释一下,以便我能正确理解?
    • executorService.submit() 一个真正无效的方法吗? doNothing().when(..) 方法应该写成:doNothing().when(executorService).submit(any(AuditExecutor.class)); 注意,when 子句只包含服务,方法写在子句之后。
    • 你好@convexmethod,提交是一个内置函数。请看上面的帖子。我已经更新了帖子并提到了提交方法定义。
    • 嗨@convexmethod,感谢您的回答,但仍有疑问。我需要使用 Callable.class 还是 AuditExecutor.class。如果我使用 Callable.class 获取 CTE - ExecutorService 类型中的方法 submit(Callable) 不适用于参数 (Matcher)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2015-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多