java 尽管有期望方法声明,EasyMock“意外的方法调用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31966388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EasyMock "Unexpected method call" despite of expect method declaration
提问by apex39
My EasyMock's expected method is perceived as unexpected, although I do not use and strict mocks, and the method is already declared before being replied.
我的 EasyMock 的预期方法被认为是意外的,虽然我没有使用和严格的模拟,并且该方法在被回复之前已经声明。
Test fails in this line of code:
在这行代码中测试失败:
Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
Test:
测试:
@Before
public void setUp() {
mocksControl = createControl();
contextMock = mocksControl.createMock(Context.class);
//(...)
}
@Test
public void test() {
expect(contextMock.getApplicationContext()).andReturn(contextMock).anyTimes();
expect(contextMock.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)))
.andReturn(someIntent1).once();
expect(contextMock.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)))
.andReturn(someIntent2).once();
mocksControl.replay();
//(...) tested method is invoked on class under the test
}
Error I get:
我得到的错误:
java.lang.AssertionError:
Unexpected method call Context.registerReceiver(null, android.content.IntentFilter@c009614f):
Context.registerReceiver(null, android.content.IntentFilter@c009614f): expected: 1, actual: 0
Context.registerReceiver(null, android.content.IntentFilter@c009614f): expected: 1, actual: 0
回答by Vihar
when you write something like,
当你写一些类似的东西时,
expect(contextMock.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)))
.andReturn(someIntent1).once();
Easymock expects the registerReceiver
method to be called with exact parameter with which it is told to expect,
Easymock 期望registerReceiver
使用被告知期望的确切参数调用该方法,
So to avoid this ,while expecting any method and writing its behaviour, use anyObject() method like this:-
因此,为了避免这种情况,在期望任何方法并编写其行为时,请使用 anyObject() 方法,如下所示:-
expect(contextMock.registerReceiver(null, EasyMock.anyObject(IntentFilter.class)))
.andReturn(someIntent1).once();
by this, easymock understands that it has to mock all the calls to expected method, when any object of IntentFilter
is passed as a parameter
通过这个,easymock 明白它必须模拟所有对预期方法的调用,当任何对象IntentFilter
作为参数传递时
Hope this helps! Good luck!
希望这可以帮助!祝你好运!
回答by Henri
By default, EasyMock use an equal matcher. So it means that the IntentFilter parameter will be compared using equals.
默认情况下,EasyMock 使用相等匹配器。所以这意味着 IntentFilter 参数将使用 equals 进行比较。
I'm not sure a working equals was coded on IntentFilter. Looking at the documentation, it's probably not the case. So this is why nothing matches.
我不确定在 IntentFilter 上是否编码了一个有效的 equals。查看文档,情况可能并非如此。所以这就是为什么没有匹配的原因。
The only surprising thing is that the toString on IntentFilter used to show the error message is the one of Object. Both all three have the same address (c009614f). Which is weird because it would mean that they all are the same instance. Which is impossible. So I'll stick with my answer.
唯一令人惊讶的是,用于显示错误消息的 IntentFilter 上的 toString 是 Object 之一。这三个都具有相同的地址 (c009614f)。这很奇怪,因为这意味着它们都是同一个实例。这是不可能的。所以我会坚持我的答案。
To fix it, depending if you really care about the parameter, you could use anyObject() or a dedicated comparator
要修复它,取决于您是否真的关心参数,您可以使用 anyObject() 或专用比较器
回答by Marcelus
I recently had a similar problem. I noticed that Easymock does not allow mixing of types. The same type strategy must be used for all parameters.
我最近遇到了类似的问题。我注意到 Easymock 不允许混合类型。所有参数必须使用相同的类型策略。
This exception usually occurs when matchers are mixed with raw values when recording a method:
当记录方法时匹配器与原始值混合时,通常会发生此异常:
foo(5, eq(6)); // wrong
You must not use a matcher at all or a matcher for every single param:
您根本不能使用匹配器或为每个参数使用匹配器:
foo(eq(5), eq(6)); // right
foo(5, 6); // also right