Skip to content
Advertisement

Mockito: How do I verify that the array passed to my method contains the right object?

I’m using Mockito 1.9.5. I want to verify that my method (which takes an array as a param) was called in which the array contains exactly one specific object. I’m having trouble figuring out how to do this. I have

Mockito.doReturn(new SaveResult[]{}).when(mockConnection).update(org.mockito.Matchers.any(SObject[].class));
…     

Mockito.verify(mockConnection, Mockito.times(1)).update( new Account[]{ acct });

Unsurprisingly, the second line fails because although the argument, “acct” is the same as what is passed, the enclosing array is not. What is the best way to check for this?

Advertisement

Answer

Mockito has a builtin matcher, AdditionalMatchaer#aryEq(T[]) for this usecase exactly:

Mockito.verify(mockConnection, Mockito.times(1))
       .update(aryEq(new Account[]{ acct }));
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement