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;
}
}));
What are Matchers? Matchers are like regex or wildcards where instead of a specific input (and or output), you specify a range/type of input/output based on which stubs/spies can be rest and calls to stubs can be verified. All the Mockito matchers are a part of 'Mockito' static class.
Mockito Argument Matcher - eq() When we use argument matchers, then all the arguments should use matchers. If we want to use a specific value for an argument, then we can use eq() method.
Matcher methods can't be used as return values; there is no way to phrase thenReturn(anyInt()) or thenReturn(any(Foo. class)) in Mockito, for instance. Mockito needs to know exactly which instance to return in stubbing calls, and will not choose an arbitrary return value for you.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With