JMock - multiple calls with different arguments

The method I want to test calls the 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 the MethodToTest method really calls these methods with exactly these arguments. This is the code I wrote:

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

In (2) I get an "Unexpected call" - as if he could not distinguish between different arguments. So I tried this:

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

But that also did not do what I expected.

Finally, it worked:

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

So, I know that my method has been called 3 times with any Integer. Is there any way to make sure that the argument (s) that I passed is exactly ?

+6
source share
1 answer

Have you surrounded expectations with a control unit?

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

Also, did you know that jmock does not apply the sequence unless you do it explicitly?

+2
source

Source: https://habr.com/ru/post/917199/


All Articles