Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JMock - several invocations with different arguments

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?

like image 832
Darek Kay Avatar asked Jun 01 '12 15:06

Darek Kay


1 Answers

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?

like image 151
Steve Freeman Avatar answered Oct 04 '22 05:10

Steve Freeman