【问题标题】:How to match 'any' parameter type while mocking private method in Jmockit如何在 Jmockit 中模拟私有方法时匹配“任何”参数类型
【发布时间】:2014-11-18 12:45:39
【问题描述】:

在以下场景中使用 jmockit 时遇到问题。在网上做了一个研究,但还没有找到答案。

在记录阶段,我将期望设置在部分模拟的对象上。在这样做时,我想模拟一个带有单个参数的私有方法。但我并不真正关心该参数值。我想将该特定私有方法的所有调用与传递给它的任何参数实例相匹配。我如何在 Jmockit 中做到这一点。有什么办法吗?

new Expectations(student) {
    {
        Deencapsulation.invoke(student, "setDepartment", (Department) any);
        result = new Delegate<Student>() {
            public void setDepartment(Department dept) {
                System.out.println("Mocked setDepartment() methodd.....");
            }
        };
    }
};  

在上面的代码中,(Department) any不能被传递,因为Deencapsulation.invoke(...)方法不接受null的值。

【问题讨论】:

    标签: jmockit private-methods partial-mocks


    【解决方案1】:

    注意any 字段的API documentation 表示:

    “在调用不可访问的方法或构造函数时(例如,使用 Deencapsulation.invoke(Object, String, Object...)),请改用 withAny(T)。”

    也就是说,您需要将withAny(Department.class)invoke(...) 调用一起使用。

    【讨论】:

      【解决方案2】:

      从 JMockit v1.49 开始,我使用:

      withInstanceOf(Department.class)
      

      它按预期工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-05-22
        • 2016-07-11
        • 1970-01-01
        • 1970-01-01
        • 2014-08-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多