views:

207

answers:

1

Hello.

I've googled about this, but didn't find anything relevant. I've got something like this:

Object obj = getObject();
Mockeable mock= Mockito.mock(Mockeable.class);
Mockito.when(mock.mymethod(obj )).thenReturn(null);

Testeable obj = new Testeable();
obj.setMockeable(mock);
command.runtestmethod();

Now, I want to verify that mymethod(Object o), which is called inside runtestmethod(), was called with the Object o, not any other. But I always pass the test, whatever I put on the verification, for example, with:

Mockito.verify(mock.mymethod(Mockito.eq(obj)));

or

Mockito.verify(mock.mymethod(Mockito.eq(null)));

or

Mockito.verify(mock.mymethod(Mockito.eq("something_else")));

I always pass the test. How can I accomplish that verification (if possible)?

Thank you.

A: 
  • You don't need the eq matcher if you don't use other matchers.
  • You are not using the correct syntax - your method call should be outside the .verify(mock). You are now initiating verification on the result of the method call, without verifying anything (not making a method call). Hence all tests are passing.

You code should look like:

Mockito.verify(mock).mymethod(obj);
Mockito.verify(mock).mymethod(null);
Mockito.verify(mock).mymethod("something_else");
Bozho
I'd tried that before, and again now to be sure. I still have the same problem, the test always passes.
manolowar
then perhaps you are not showing enough. This should work.
Bozho