6
votes

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?

1
Well, the posted code works just fine. JMock seems to have problems with casted objects, though.. Solved so far.Darek Kay
what are the casting problems? Can you give us more detail?Steve Freeman

1 Answers

2
votes

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?