这是我的示例代码示例,当我进行jmockit更新相关的更改时,该示例开始失败。
class A {
public static boolean validate(String name, int age, boolean flag) {
boolean result = false;
//actual code
return result;
}
}
class B {
public void cal() {
if (A.validate(name, age, flag)) {
// some calculations
}
}
}
class TestB {
public B b;
@Before
public void setUp() {
b = new B();
}
@Test
public void testCal() {
new Expectations() {
{
A.validate(anyString, anyInt, anyBoolean);
times= -1;
result = Boolean.TRUE;
}};
b.cal();
new Verifications() {
{
A.validate(anyString, anyInt, anyBoolean);
}
};
}
}
这失败并显示错误
mockit.internal.UnexpectedInvocation: Unexpected invocation of:
A#validate(String name, int age, boolean flag)
我刚刚将NonStrictExpectations块更改为Expectations块,因为最新的jmockit不支持 NonStrictExpectations块。
new NonStrictExpectations() {
{
A.validate(anyString, anyInt, anyBoolean);
returns(Boolean.TRUE);
}
};
使用此块,一切正常。
请让我知道问题出在哪里?
答案 0 :(得分:1)
首先,对times
使用负值是无效值。
第二,如果需要进行可选调用,则应使用minTimes
和maxTimes
变量。对于使用maxTimes = 1
的情况,您将实现一个可选的模拟调用。