有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java IllegalArgumentException:预期块内的条件语句无效

我在测试用例中编写的Expectations块有问题:

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) 个答案

  1. # 1 楼答案

    从版本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.

    在JMockitsource中,您可以看到哪些其他类型的条件和循环将引发该异常

    简而言之,从JMockit 1.14开始,不允许在期望块中有条件语句(如if语句)和循环