Mockito UnfinishedStubbingException [英] Mockito UnfinishedStubbingException

查看:329
本文介绍了Mockito UnfinishedStubbingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Mockito的新手,我试过调查这个例外但我还没找到具体的答案。当我一起使用两个模拟时,它发生在我的代码中,这意味着我通过模拟的构造函数,另一个模拟。像这样:

I am new to Mockito, I have tried looking into this Exception but I haven´t found a concrete answer. It happens in my code when I use two mocks together, meaning that I give through the constructor of a mock, another mock. Like so:

...
OperationNode child = getNode(Operation.ADD);
child.insertNode(getConstantNode(getIntegerValue(2));
...

 private ConstantNode getConstantNode(NumericalValue value){
    ConstantNode node = Mockito.mock(ConstantNode.class);
    Mockito.when(node.evaluate()).thenReturn(value);
    Mockito.when(node.toString()).thenReturn(value.toString());
    return node;
}

private IntegerValue getIntegerValue(int number) {
   IntegerValue integerValue = Mockito.mock(IntegerValue.class);
   Mockito.when(integerValue.getValue()).thenReturn(number);
   Mockito.when(integerValue.toString()).thenReturn(Integer.toString(number));
   return integerValue;
}

在我读过的一个论坛中,我没有通过另一个模拟的构造函数发送模拟,因为Mockito可能会与模拟调用混淆,所以我尝试了以下内容:

In one of the forums I read about not sending a mock through a constructor of another mock, since Mockito might get confused with the mock calls, so I tried the following:

NumericalValue value = getIntegerValue(2);
child.insertNode(getConstantNode(value));

但无济于事。我确保只调用方法 toString() getValue(),因为这些是唯一的方法上课了。我不明白发生了什么。

But to no avail. I make sure that only the methods toString() and getValue() are called, because those are the only methods the class has. I don´t understand what´s going on.

我也试过单独使用模拟,看看我做错了什么:

I have also tried using the mocks separately, to see if I have done something wrong:

child.insertNode(new ConstantNode(getIntegerValue(2)));

完美无缺。

child.insertNode(getConstantNode(new IntegerValue(2)));

也行得正常。

推荐答案

从我在mockito的问题53上读到的内容( https://code.google.com/p/mockito/issues/detail?id=53 ),由于Mockito中涉及的验证框架,我的代码遇到了问题。确切地说,以下代码本身导致异常。

From what I read on "Issue 53" of mockito (https://code.google.com/p/mockito/issues/detail?id=53) , my code was experiencing a problem due to the validation framework involved in Mockito. Precisely the following code was causing the exception per se.

private ConstantNode getConstantNode(NumericalValue value){
    ConstantNode node = Mockito.mock(ConstantNode.class);
    Mockito.when(node.evaluate()).thenReturn(value);
    Mockito.when(node.toString()).thenReturn(value.toString());
    return node;
}

如果你记得我的代码,参数值也是一个MOCK,所以当 value.toString() thenReturn()上调用时,我相信(如果有人请纠正我,如果我错了)验证框架被触发并确保每个when已经有 thenReturn()被调用/验证/等。因此,如果发生这种情况, Mockito.when(node.toString())。thenReturn(value.toString() 将无法验证,因为它没有从 valute.toString()返回,它启动了整个验证一切链。

If you remember from my code, the parameter value is ALSO A MOCK, so that when value.toString() is called on the thenReturn(), I believe (and someone please correct me if I am wrong) that the validation framework is triggered and makes sure that every "when" has had its thenReturn() called/validated/etc. So that if this happenes, the Mockito.when(node.toString()).thenReturn(value.toString() will not be validated because it hasn´t returned from the valute.toString(), which started the whole "validate everything" chain.

我是如何修复的:

private ConstantNode getConstantNode(NumericalValue value){
    ConstantNode node = Mockito.mock(ConstantNode.class);
    Mockito.when(node.evaluate()).thenReturn(value);

    String numberToString = value.toString();

    Mockito.when(node.toString()).thenReturn(numberToString);
    return node;
}

通过这种方式,可以进行验证。我发现这是一个完整的代码味道,因为我将不得不留下一条评论,解释为什么我在使用一个看似无用的中间变量代码。

This way, it can be validated. I find this a complete code smell because I will literally have to leave a comment that explains why I am using a seemingly useless intermediate variable in the code.

感谢您的帮助。

这篇关于Mockito UnfinishedStubbingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆