JMockit教程(官方文档翻译版)

2.2 Expectations

2016-12-27  本文已影响57人  孙兴斌

Expectations中定义了mock对象将会被调用的方法以及方法的返回值。Expectations中出现的方法必须被调用,而调用的方法不必全部出现在Expectation中。

但是,如果定义了mock对象,并在测试代码中调用了它的某个方法,而该方法没有出现在Expectation中,JMockit并不会执行其原有代码,而是返回null或者原始类型的初始值。例如:

public class CodeUnderTest {
    public int testMethod() {
        Dependency dependency = new Dependency();
        return dependency.mockMethod();
    }
}
public class Dependency {
    public int mockMethod() {
        return 1;
    }
}
@RunWith(JMockit.class)
public class MyTest {

    @Mocked
    Dependency dependency;

    @Test
    public void TestMethod() throws Exception {
        CodeUnderTest codeUnderTest = new CodeUnderTest();
        assertEquals(0, codeUnderTest.testMethod());
    }
}
上一篇 下一篇

猜你喜欢

热点阅读