289

With Mockito, I want to verify() a method call with byte[] in its argument list, but I didn't find how to write this.

 myMethod( byte[] )

I just want something like anyByteArray(), how to do that with Mockito ?

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • Do you really not care what the contents of the byte array are? I see this a lot in unit testing, where people use anyX() matchers because they're convenient, but realistically you almost always should care what's being passed in. If you're not using an Answer that actually consumes the value, you probably should match on an actual expected argument. – Matunos Jul 23 '15 at 01:12
  • 1
    @Matunos: That's debatable. Using the any-matchers can make tests simpler and the next person looking at the test will not be distracted by unnecessarily precise matching and can focus on the actual purpose of the test. – Malik Atalla Dec 21 '15 at 22:50
  • @tbruelle: Please keep in mind that every array in Java is an object. That would help you at the begining. – Tomasz Przybylski Mar 10 '16 at 07:51

8 Answers8

547

I would try any(byte[].class)

gpeche
  • 21,974
  • 5
  • 38
  • 51
49

Try this:

AdditionalMatchers.aryEq(array);
Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
16

I would rather use Matchers.<byte[]>any(). This worked for me.

Fabiano Francesconi
  • 1,769
  • 1
  • 19
  • 35
  • 3
    Warning for everybody: Now is deprecated so I'd go for the voted answer Mockito.any(byte[].class) – Chexpir Sep 17 '18 at 09:19
13

I agree with Mutanos and Alecio. Further, one can check as many identical method calls as possible (verifying the subsequent calls in the production code, the order of the verify's does not matter). Here is the code:

import static org.mockito.AdditionalMatchers.*;

    verify(mockObject).myMethod(aryEq(new byte[] { 0 }));
    verify(mockObject).myMethod(aryEq(new byte[] { 1, 2 }));
Rene Ummels
  • 223
  • 3
  • 8
2

I used Matchers.refEq for this.

Bowofola
  • 1,132
  • 9
  • 12
0

You can use Mockito.any() when arguments are arrays also. I used it like this:

verify(myMock, times(0)).setContents(any(), any());
Pod
  • 3,938
  • 2
  • 37
  • 45
Crenguta S
  • 526
  • 9
  • 11
0

You can always create a custom Matcher using argThat

Mockito.verify(yourMockHere).methodCallToBeVerifiedOnYourMockHere(ArgumentMatchers.argThat(new ArgumentMatcher<Object>() {
    @Override
    public boolean matches(Object argument) {
        YourTypeHere[] yourArray = (YourTypeHere[]) argument;
        // Do whatever you like, here is an example:
        if (!yourArray[0].getStringValue().equals("first_arr_val")) {
            return false;
        }
        return true;
    }
}));
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • Writing a custom matcher, when Mockito is capable of handling this use case is only adding technical debt. – linuxdan Nov 30 '18 at 17:01
  • 1
    In my use case, I needed to match an array, of which it was not possible to know the order. I could not find a Mockito matcher which matched arrays _order agnostic_, for this case I found the `argThat(ArgumentMatcher` worked well and didn't add much complexity. – Bill Naylor Oct 29 '20 at 08:36
  • 1
    Maybe useful in cases where you want to verify the first few bytes, etc... – rogerdpack Jan 06 '21 at 15:25
0

What works for me was org.mockito.ArgumentMatchers.isA

for example:

isA(long[].class)

that works fine.

the implementation difference of each other is:

public static <T> T any(Class<T> type) {
    reportMatcher(new VarArgAware(type, "<any " + type.getCanonicalName() + ">"));
    return Primitives.defaultValue(type);
}

public static <T> T isA(Class<T> type) {
    reportMatcher(new InstanceOf(type));
    return Primitives.defaultValue(type);
}