java - IllegalArgumentException:期望 block 内的条件语句无效

标签 java junit jmockit illegalargumentexception expectations

我在测试用例中编写的 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/

相关文章:

java - 测试套件在 JUnit5 中是否被认为已弃用?

java - JMockit 依赖构造函数

java - Java中的跳跃算法

java - junit 测试用例生成器

java - JUnit 在没有 @BeforeEach 的情况下创建该类的新实例?

jmockit - 如何在 Jmockit 中模拟私有(private)方法时匹配 'any' 参数类型

java - 如何将输入发送到 Junit 中的 System.console.readLine(...)?

java - 评估后缀表达式,例如

java - 传递参数时获取 java.lang.NullPointerException

java - EMR 中的多个输入和多个映射器类(EMR 中是否有类似 Hadoop 上的 MultipleInputs 的东西)