9

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
apex39
  • 603
  • 1
  • 6
  • 20

4 Answers4

9

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!

Vihar
  • 3,626
  • 2
  • 24
  • 47
  • 2
    Thank you, it helped! Then I received `IllegalStateException: 2 matchers expected, 1 recorded` what contributed to this shape of final solution: `expect(contextMock.registerReceiver((BroadcastReceiver) isNull(), anyObject(IntentFilter.class))) .andReturn(someIntent).once();` – apex39 Aug 13 '15 at 07:30
3

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

Henri
  • 5,551
  • 1
  • 22
  • 29
  • Thank you for the technical insight :) Is it possible for EasyMock to have feature of checking if working equals is coded in the object? – apex39 Aug 13 '15 at 07:33
  • Interesting idea. Can you please fill a feature request here? https://github.com/easymock/easymock/issues – Henri Aug 15 '15 at 02:36
2

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.

0

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
Jan B.
  • 6,030
  • 5
  • 32
  • 53
Marcelus
  • 31
  • 1
  • 2