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
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!