6

The method I want to test is calling a mock method with different arguments:

public void methodToTest(){
   getMock().doSomething(1);
   getMock().doSomething(2);
   getMock().doSomething(3);
}

In my unit test I want to know, if methodToTest really is calling those methods with exactly those arguments. This is the code I wrote:

@Test
public void myMockTest(){
    oneOf(mock).doSomething(1);
    oneOf(mock).doSomething(2);
    oneOf(mock).doSomething(3);
}

At (2) I get an "Unexpected invocation" - as if it couldn't distinguish different arguments. So I've tried that one:

exactly(3).of(mock).doSomething(with(Matchers.anyOf(same(1), same(2), same(3))));

But this also didn't do what I've expected.

Finally, this one worked:

exactly(3).of(mock).doSomething(with(any(Integer.class)));

So I know, that my method was called 3 times with any Integer number. Is there any way to make sure, it's exactly the argument(s) I have passed?

Darek Kay
  • 61
  • 2

1 Answers1

2

Did you surround the expectations with a checking block?

context.checking(new Expectations() {{
  oneOf(mock).doSomething(1);
  oneOf(mock).doSomething(2);
  oneOf(mock).doSomething(3);
}});

Also, are you aware the jmock does not enforce sequence unless you do so explicitly?

Steve Freeman
  • 2,707
  • 19
  • 14
  • I used the correct syntax and JMock is using a default sequence without having to say so. I'm still not sure, what was causing the problems - I'm using a list and adding an element. Asserting equality to the object and the object got by list.get(0) returned false, altough it IS the same object. Overriding equals/hashcode in my object class solved the problem. – Darek Kay Jun 06 '12 at 14:37