Override eq for byte array.

2,655 views
Skip to first unread message

nrichand

unread,
Jun 4, 2009, 3:51:09 AM6/4/09
to mockito
Hi!

I use Mockito to verify that one method is called and with the exact
parameters.

I would like to do this :
Mockito.verify(mock).myMethod(eq(myByteArray));

But it seems that eq() compare the reference and not the content (with
an Arrays.equals() ).
I have found a solution with argumentMatcher, but this solution is
complex for the team and not very readable :

ArgumentMatcher<byte[]> eqByte = new ArgumentMatcher<byte[]>()
{
public boolean matches(Object bytes) {
return Arrays.equals(expectedContent, (byte[]) bytes);
}
};

Mockito.verify(ejb).myMethod(argThat(eqByte));

So my question is : is it possible to override the default behavior of
eq() for byte[]?
And also, could you add this feature in a future release for any array
of primitives?

thx for this great API,
Nathaniel

szczepiq

unread,
Jun 4, 2009, 5:16:54 AM6/4/09
to moc...@googlegroups.com
Hi,

I see what you mean. Mockito takes the generic approach and eq() just
does the object.equals(otherObject).

Have a look at AdditionalMatchers - it contains aryEq() matchers.

>So my question is : is it possible to override the default behavior of eq() for byte[]?
>And also, could you add this feature in a future release for any array of primitives?

File an enhancement report for it in the issue tracker and we will
think about it.

Cheers,
Szczepan Faber

Richard Wallace

unread,
Jun 4, 2009, 9:52:16 AM6/4/09
to moc...@googlegroups.com

Instead of using your own matcher, you could just use the existing
Hamcrest matcher which does the right thing with arrays.

import static org.hamcrest.Matchers.*;

verify(ejb).myMethod(argThat(is(equalTo(myByteArray))));

Rich

nrichand

unread,
Jun 8, 2009, 9:39:06 AM6/8/09
to mockito
Ok, I use the additional matcher which is the easiest.

Thx for the suggestions.
Nathaniel
Reply all
Reply to author
Forward
0 new messages