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 performExecution
based on the field1, but there is no direct way to do it even with the ArgumentMatchers.
Suppose I mocked the class:
MyClass myClass = mock(MyClass.class)
Now, I want to perform something like this:
when the Params
object passed in myClass.performExecution()
has the field1
value as "1"
, then give a certain response, otherwise some other response.
Is that a way to do that ?
There are a couple of options:
Option 1: Custom argument matcher
See for example Custom Argument Matcher by baeldung
Option 2: when / thenAnswer
Option 3: argThat Matcher
Note null checks in argThat matchers