How do I write a mockito matcher for byte []?

I need a complex Matcher

for byte[]

. The code below does not compile as it argThat

returns byte[]

. Is there a way to write a dedicated Matcher

array of primitive types?

    verify(communicator).post(Matchers.argThat(new ArgumentMatcher<Byte[]>() {

        @Override
        public boolean matches(Object argument) {
            // do complex investigation of byte array
            return false;
        }
    }));

      

+3


source to share


1 answer


You can actually use new ArgumentMatcher<byte[]> { ... }

here:

verify(communicator).post(Matchers.argThat(new ArgumentMatcher<byte[]>() {
    @Override
    public boolean matches(Object argument) {
        // do complex investigation of byte array
        return false;
    }
}));

      



The answers you referenced say that byte[]

it is not a valid substitution for T[]

(because it T[]

accepts Object[]

which is byte[]

not), but in your case it is not T[]

, and byte[]

that being a subclass Object

is a valid substitution for a simple T

.

+1


source







All Articles