[英]How to mock a method call with Mockito
i am trying to write Unittests for a MAS. 我正在尝试为MAS编写单元测试。 Therefore i have to mock the agent an agent is communicating with.
因此,我必须模拟与代理进行通信的代理。 Here is an example of what i am trying to do at the moment.
这是我目前正在尝试做的一个例子。 I have an agent A which is the one that i want to test.
我有一个代理商A,这是我要测试的代理商。 He calls B.method(arg) on an other agent B, which is mocked in my case.
他在另一个代理B上调用B.method(arg),在我的情况下这是模拟的。 Normally B would call A.method(arg1, arg2, arg3).
通常,B将调用A.method(arg1,arg2,arg3)。 I am new to Mockito and doesn't know a lot about its possibilities.
我是Mockito的新手,对它的可能性并不了解。 Is it possible to do something like when(mock.method).thenCall(A.method)?
是否可以做诸如when(mock.method).thenCall(A.method)之类的事情?
Thanks for your help! 谢谢你的帮助!
A little late to the party, but I believe would be able to achieve what you wanted with an Answer. 派对晚了一点,但是我相信通过“答案”可以实现您想要的目标。
The following will call a real method (methodOnA), whenever the mock method (methodOnB) is called: 每当调用模拟方法(methodOnB)时,以下方法都会调用一个实型方法(methodOnA):
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
realObjectA.methodOnA()
return null;
}
}).when(mockObjectB).methodOnB();
The intent of mocking a method call is to immediately return a value (or throw an exception) when the method is called on the mock instance. 模拟方法调用的目的是在模拟实例上调用该方法时立即返回一个值(或引发异常)。 It allows you to exercise the class-under-test without calling upon that class' collaborators.
它使您可以在不召唤该班级合作者的情况下锻炼被测班级。
So rather than delegate the method call to another method call, you would usually just thenResult(someValue)
or thenThrow(someException)
. 因此,通常不将方法调用委托给另一个方法调用,而是通常使用
thenResult(someValue)
或thenThrow(someException)
。
It may be that your test relies on a helper method to derive someValue
, but that's unlikely to be a useful test. 可能是您的测试依赖于辅助方法来得出
someValue
,但这不太可能是有用的测试。
You shouldn't do it, it's a bad design to ENFORCE B
to call A
(It is fine to do it, but enforcing it is wrong), the fact that B
is calling A
should be tested in B
's unitests, not A
's, and A
should be unaware of it. 您不应该这样做,这对ENFORCE
B
调用A
来说是一个糟糕的设计(可以这样做,但是强制执行是错误的),事实是B
调用A
的事实应该以B
的单位而不是A
进行测试。的, A
应该不知道。
You can on the other hand, verify with Mockito that B.method()
was invoked (and how many times, and with which argument), or alternatively return a stub value from B.method()
without invoking A
, which is a good unitest - since it checks for a single behavior of A
. 另一方面,您可以通过Mockito验证是否已调用
B.method()
(以及调用了多少次,以及使用了哪个参数),或者可以选择从B.method()
返回存根值而不调用A
,这是一个很好的选择。 unitest-因为它检查A
的单个行为。 (The call to A.method()
should be tested seperately). (对
A.method()
的调用应单独测试)。
For example, returning a stub value with Mockito is easily done with 例如,使用Mockito返回存根值很容易
Mockito.when(mockedB.method(SOME_ARG_HERE)).thenReturn(SOME_RESULT);
Much more useful examples and information can be found at Mockito's javadocs 在Mockito的javadocs中可以找到更多有用的示例和信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.