How to write mockito matcher for byte[]?

7.5k views Asked by At

I need a complex Matcher for byte[]. The code below does not compile since argThat returns Byte[]. Is there a way to write a dedicated Matcher for an 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;
        }
    }));
1

There are 1 answers

0
axtavt On

You really can 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 are referring to say that byte[] is not a valid substitute for T[] (because T[] assumes Object[], which byte[] is not), but in your case there is no T[] involved, and byte[], being a subclass of Object, is a valid substitute for simple T.