【问题标题】:Recording expectations in a method called from the instance initialiser在从实例初始化程序调用的方法中记录期望
【发布时间】:2012-11-02 17:19:57
【问题描述】:

我不太了解以下测试的行为。仔细看,test_OKtest_Not_OK 是完全等价的——唯一的区别是 test_OK 具有“内联”callMethod

但是,test_OK 通过,而 test_Not_OK 失败。这种行为有原因吗?

public class MethodCallTest {
    @Test
    public void test_Not_OK() {
        new NonStrictExpectations() {
            Whatever w;
            {
                callMethod();
            }
            private void callMethod() {
                w.method();
                result = 1;
            }
        };
        assertEquals(new Whatever().method(), 1); //fails
    }

    @Test
    public void test_OK() {
        new NonStrictExpectations() {
            Whatever w;
            {
                w.method();
                result = 1;
            }
        };
        assertEquals(new Whatever().method(), 1); //passes
    }

    public static class Whatever {
        public int method() {
            return 0;
        }
    }
}

【问题讨论】:

    标签: java jmockit expectations


    【解决方案1】:

    好问题。原因是 JMockit 对 ExpectationsNonStrictExpectations 子类的构造函数和初始化块中的字节码执行某些转换。 (本质上,这样做是为了让模拟 API 工作。例如,对特殊 result 字段的每个分配实际上都被方法调用替换,以便它与当前期望正确关联。)

    然而,期望块中的

    方法尚未转换。他们应该是,所以我现在正在实施它。从下一个版本 (0.999.19) 开始,将支持此功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2014-07-29
      • 1970-01-01
      • 2017-09-05
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多