Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify object attribute value with mockito

I have a method call which I want to mock with mockito. To start with I have created and injected an instance of an object on which the method will be called. My aim is to verify one of the object in method call.

Is there a way that mockito allows you to assert or verify the object and it's attributes when the mock method is called?

example

Mockito.verify(mockedObject)        .someMethodOnMockedObject(               Mockito.<SomeObjectAsArgument>anyObject()) 

Instead of doing anyObject() i want to check that argument object contains some particular fields

Mockito.verify(mockedObject)        .someMethodOnMockedObject(               Mockito.<SomeObjectAsArgument>**compareWithThisObject()**) 
like image 804
Priyank Avatar asked Jul 17 '09 12:07

Priyank


People also ask

What is verify method in Mockito?

Mockito verify() method can be used to test number of method invocations too. We can test exact number of times, at least once, at least, at most number of invocation times for a mocked method. We can use verifyNoMoreInteractions() after all the verify() method calls to make sure everything is verified.

How do you use argument matchers in Mockito?

Mockito requires that we provide all arguments either by matchers or exact values. There are two more points to note when we use matchers: We can't use them as a return value; we require an exact value when stubbing calls. We can't use argument matchers outside of verification or stubbing.

What can I use instead of Mockito matchers?

Since Mockito any(Class) and anyInt family matchers perform a type check, thus they won't match null arguments. Instead use the isNull matcher.

What is argThat in Mockito?

The argThat argument matcher in Mockito lets you create advanced argument matchers that run a function on passed arguments, and checks if the function returns true . If you have a complicated class that can't be easily checked using . equals() , a custom matcher can be a useful tool.


2 Answers

New feature added to Mockito makes this even easier,

ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class); verify(mock).doSomething(argument.capture()); assertEquals("John", argument.getValue().getName()); 

Take a look at Mockito documentation


In case when there are more than one parameters, and capturing of only single param is desired, use other ArgumentMatchers to wrap the rest of the arguments:

verify(mock).doSomething(eq(someValue), eq(someOtherValue), argument.capture()); assertEquals("John", argument.getValue().getName()); 
like image 151
iraSenthil Avatar answered Sep 24 '22 13:09

iraSenthil


I think the easiest way for verifying an argument object is to use the refEq method:

Mockito.verify(mockedObject).someMethodOnMockedObject(ArgumentMatchers.refEq(objectToCompareWith)); 

It can be used even if the object doesn't implement equals(), because reflection is used. If you don't want to compare some fields, just add their names as arguments for refEq.

like image 33
John29 Avatar answered Sep 23 '22 13:09

John29