我在测试用例中编写的 Expectations
block 有问题:
new Expectations() {
{
mFindHandlerMock.findAll((Model) any, (Set<Id>) any, false);
if (!pWithRealData) {
result = Collections.emptySet();
} else {
result = pAllData;
}
times = 1;
Deencapsulation.invoke(mDb, "readSqlQuery", withAny(String.class));
result = "select * from realdata";
times = 1;
}
};
测试用例崩溃:
java.lang.IllegalArgumentException: Invalid conditional statement inside expectation block
就在这里:
if (!pWithRealData) {
这只是一个简单的 boolean 值
,在本例中为false
。
我完全不知道为什么会发生exception
。
我已经用谷歌搜索过,但没有找到任何帮助。
你能帮帮我吗?
最佳答案
来自 1.14 版的 JMockit 发行说明:
Enhancement: Conditionals and loops will now trigger an exception when found inside an expectation recording block, to prevent API misuse and to encourage simpler tests. See issue #97.
与此相关的GitHub问题:
在一期中,他们声明:
Yes, and this is as intended, to avoid tests getting too complicated when recording expectations. A full test was not shown, but it looks to me that recording the specific expectations directly would be better in this case.
在 JMockit source您可以看到哪些其他类型的条件和循环会抛出该异常。
简而言之,从 JMockit 1.14 开始,您不允许在 Expectation block 中使用条件(例如 if 语句)和循环。
关于java - IllegalArgumentException:期望 block 内的条件语句无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36055260/