0 votes
1 view
in Java by (10.2k points)

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 testableObj = new Testeable();

testableObj.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.

1 Answer

0 votes
by (46.1k points)

An alternative to ArgumentMatcher is ArgumentCaptor.

Official example:

ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);

verify(mock).doSomething(argument.capture());

assertEquals("John", argument.getValue().getName());

A captor can also be defined using the @Captor annotation:

@Captor ArgumentCaptor<Person> captor;

//... MockitoAnnotations.initMocks(this);

@Test public void test() {

    //...

    verify(mock).doSomething(captor.capture());

    assertEquals("John", captor.getValue().getName());

}

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...