gpt4 book ai didi

java - Mockito 和延迟结果

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:32:22 25 4
gpt4 key购买 nike

我有一个基于 spring 的项目,我正在努力提高其中的代码覆盖率

我有以下代码块,它在 defferedResult onCompletion 方法上使用了 lambda

        util.getResponse(userInfoDeferredResult, url, userName, password);

userInfoDeferredResult.onCompletion(() -> {
//the result can be a String or ErrorResponse
//if ErrorResponse we don't want to cause ClassCastException and we don't need to cache the result
if (userInfoDeferredResult.getResult() instanceof String){

String response = (String) userInfoDeferredResult.getResult();

cacheServices.addValueToCache(Service.USER_INFO_CACHE_NAME, corpId, response);
}
});

我想知道 - 是否可以使用 mockito 或 powerMockito 模拟 onCompletion lambda 的内容?

最佳答案

将内容提取到新方法:

if(userInfoDeferredResult.getResult() instanceof String) {
String response = (String) userInfoDeferredResult.getResult();
cacheServices.addValueToCache(Service.USER_INFO_CACHE_NAME, corpId, response);
}

然后这样测试方法?

关于java - Mockito 和延迟结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471734/

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