EasyMock “Unexpected method call” despite of expect method declaration

元气小坏坏 提交于 2019-12-12 10:53:20

问题


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.

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

回答1:


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,

So to avoid this ,while expecting any method and writing its behaviour, use anyObject() method like this:-

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

Hope this helps! Good luck!




回答2:


By default, EasyMock use an equal matcher. So it means that the IntentFilter parameter will be compared using 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.

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.

To fix it, depending if you really care about the parameter, you could use anyObject() or a dedicated comparator




回答3:


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.

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


来源:https://stackoverflow.com/questions/31966388/easymock-unexpected-method-call-despite-of-expect-method-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!