270

使用 Mockito,我想在其参数列表中verify()调用方法byte[],但我没有找到如何编写它。

 myMethod( byte[] )

我只是想要类似的东西anyByteArray(),如何用 Mockito 做到这一点?

4

8 回答 8

508

我会尝试any(byte[].class)

于 2012-04-08T21:42:26.507 回答
42

尝试这个:

AdditionalMatchers.aryEq(array);
于 2014-05-30T11:46:11.120 回答
14

我宁愿使用Matchers.<byte[]>any(). 这对我有用。

于 2015-01-29T13:42:32.577 回答
12

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 }));
于 2015-10-25T09:53:09.343 回答
2

我用Matchers.refEq这个。

于 2018-02-16T19:36:03.033 回答
0

当参数也是数组时,您可以使用 Mockito.any() 。我这样使用它:

verify(myMock, times(0)).setContents(any(), any());
于 2016-07-05T10:55:57.977 回答
0

您始终可以使用创建自定义匹配器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;
    }
}));
于 2018-10-02T14:42:42.970 回答
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);
}
于 2019-10-28T16:30:26.343 回答