【问题标题】:Why always Jmockit mock interface return null, though i have specify result?为什么 Jmockit 模拟接口总是返回 null,尽管我已经指定了结果?
【发布时间】:2021-02-10 11:00:43
【问题描述】:

jmockit 1.46 版 爪哇 1.8 简单的代码如下:

class PhoneControllerTest{

@Tested
PhoneController phoneController;

@Injectable
IPhoneService phoneService;
....
public void testMethod(@Tested Map map){
new Expections{{
  // example 1.mock success when param is 
  // primitive
  phoneService.call("12345");
  result="0";

  //example 2 .mock fail when param is 
  
   phoneService.call(map);
   result="0";

  
}}  
}}
interface IPhoneService{ 
     String call(String id);
     String call(Map map);
}

我对example2感到困惑,为什么它返回null而不是0?我应该怎么做result在example2中运行良好。 @injectable 对“结果”有影响吗?

【问题讨论】:

  • 您在Expectations 中输入的Complex 对象可能不等于您稍后在测试中输入的对象。你能展示完整的示例代码和Complex 类吗?否则它只是在猜测可能出了什么问题。
  • 我已经更新了代码。谢谢

标签: java jmockit


【解决方案1】:

我已经解决了这个问题,只需将复杂的参数替换为 any。 phoneService.call((Map)any);

【讨论】:

    猜你喜欢
    • 2013-10-31
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多