Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock "Unexpected method call" despite of expect method declaration

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
like image 764
apex39 Avatar asked Aug 12 '15 13:08

apex39


3 Answers

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!

like image 171
Vihar Avatar answered Nov 18 '22 03:11

Vihar


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

like image 35
Henri Avatar answered Nov 18 '22 02:11

Henri


For people running into this issue, note that the number of times a source code method is called within a test should be equal to the number of times an expect is set.

For eg: if the following expectation is set in test code,

expect(contextMock.registerReceiver(null, EasyMock.anyObject(IntentFilter.class)))
        .andReturn(someIntent1).once();

that means, when the test code is run, it should have exactly 1 call to the registerReceiver method. Otherwise, we would end up with different assertion exceptions like so:

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

The expected and actual numbers start varying depending on the number of calls.

like image 1
Sudhir Vaidya Avatar answered Nov 18 '22 02:11

Sudhir Vaidya