All test frameworks come with at least simple matching capabilities, though the syntax, usage and breadth of coverage may differ. Most frameworks also provide a mechanism to create and use "custom" matchers.
Questions tagged [argument-matcher]
19 questions
6
votes
3 answers
Mockito doesn't correctly stub methods taking list as argument
I am trying to mock a class and return a stubbed list of objects when a method on the mocked object is called. Lets consider following code :
interface MyRepositry{
public List getMyClassInstances(String str,Long id,List…

Shailesh Vaishampayan
- 1,766
- 5
- 24
- 52
5
votes
7 answers
mocking resttemplate exchange always returns null
I'm trying to mock a restTemplate.exchange method through mockito but it always returns a null value no matter what i return through mock , even if i throw an exception:
this is the actual code :
ResponseEntity
- > response =…

avi
- 175
- 2
- 5
- 17
3
votes
2 answers
Mockito anyListOf() List>
I'm using mockito-core:2.8.47 and Java 7 and want to use in a when and verify anyListOf or some other any method.
My Problem is, if I just use anyList it says:
The method name( int, List < List < String > >) in the type Y is not
applicable for…

Ganymed
- 79
- 1
- 2
- 7
2
votes
1 answer
Mockito ArgumentMatcher any Object[]
I need an ArgumentMatcher accepting any type of values in an Object array (Object[]).
TestObject:
class ObjectArrayMethod {
public int call(Object... objects) {
return 0;
}
}
I tried these, where all assertEquals are failing:
…

Markus Kreth
- 748
- 7
- 26
2
votes
1 answer
Mockito error with custom matcher
I have a Java class:
import java.util.List;
public class Service
{
public List

orbfish
- 7,381
- 14
- 58
- 75
1
vote
0 answers
When to use and not to use Mockito Argument Matchers. difference between any(), anyString(), Mockito.anyString(), Mockito.any()?
Hi I am very new to mockito framework. Can you please explain in lay man term why we need argument matchers and what is the need of any(), anyString() and what if I use a correct string instead of anyString() will it logically correct? And why these…

Santrupta Dash
- 269
- 2
- 15
1
vote
0 answers
Matching value in Map for using in Mockito.when
Dears, I'm trying to using Mockito to create different mocks based on a value contained in a Map.
Following the documentation I created two different ArgumentMatchers that run the logic
private class ConsumerValidMap implements…

jz9302
- 11
- 1
1
vote
0 answers
NPE in Mockito argument matcher with doReturn causing all other tests to fail
I am using TestNG along with Mockito for my test classes. I tried to create a spy object and used ArgumentMatchers to stub a method call. While using argument matchers, Mockito states that to match a primitive type we need to use…

Gautham M
- 4,816
- 3
- 15
- 37
1
vote
3 answers
how to pass in multiple ArgumentMatchers to Mockito
I have two custom ArgumentMatchers and I'd like my mock to return a different value based on the argument value.
Example:
when(myMock.method(new ArgMatcher1()).thenReturn(false);
when(myMock.method(new…

Chris Morris
- 4,335
- 4
- 24
- 28
1
vote
2 answers
Alternative to extending ArgumentMatcher for argument verification for mockito
I was wondering if there is more elegant way to verify the arguments other than using a class that extends ArgumentMatcher?
Thanks.

user_1357
- 7,766
- 13
- 63
- 106
1
vote
1 answer
Unable to verify subset of arguments passed to mocked method in specs2
Here's a simple specification reproducing the issue:
package ro.igstan.learning
import org.specs2.matcher.ThrownExpectations
import org.specs2.mock.Mockito
import org.specs2.mutable.Specification
import org.specs2.specification.Scope
class…

Ionuț G. Stan
- 176,118
- 18
- 189
- 202
0
votes
1 answer
Mocking method behaviour based on the field of the parameter Java 8
I have a class definition as follows:
public class MyClass {
public performExecution(ParamsClass paramsClassObject);
}
public ParamsClass {
private String field1;
private String field2;
}
Now, I want to mock the method…

GlidingSwords997
- 61
- 7
0
votes
1 answer
Mockito Argument Matcher any() - Argument(s) are different! for verify method called with any()
I am trying to write a test where I am very simply checking if a method was called on a mock. Should be easy, I've done it many times using verify, however for some reason my test is failing because it seems to be comparing the actual invocation…

jet
- 415
- 1
- 4
- 8
0
votes
2 answers
Mockito argThat does not work when used to match an iterator arg
I am trying to verify a mocked service was called with an arguments in which the last is an iterator. This is the assertion in the test:
verify(myService).myMethod(
...,
argThat(dataIterator -> iteratorEquals(dataIterator,…

Tom Carmi
- 385
- 1
- 5
- 18
0
votes
1 answer
Is it possible using variables of ArgumentMatchers?
Let's say I have an interface looks like this.
interface Some {
/**
* Does some on specified array with specified index and returns the array.
*
* @param array the array.
* @param index the index.
* @returns given…

Jin Kwon
- 20,295
- 14
- 115
- 184