title: Mockito return value based on property of a parameter
body: <p>Normally when using Mockito I will do something like:</p>
  <pre><code>Mockito.when(myObject.myFunction(myParameter)).thenReturn(myResult);
  </code></pre>
  <p>Is it possible to do something along the lines of</p>
  <pre><code>myParameter.setProperty(&quot;value&quot;);
  Mockito.when(myObject.myFunction(myParameter)).thenReturn(&quot;myResult&quot;);
  
  myParameter.setProperty(&quot;otherValue&quot;);
  Mockito.when(myObject.myFunction(myParameter)).thenReturn(&quot;otherResult&quot;);
  </code></pre>
  <p>So rather than when just using the parameter to determine the result. It is using a value of a property inside the parameter to determine the result.</p>
  <p>So when the code is executed it behaves like so:</p>
  <pre><code>public void myTestMethod(MyParameter myParameter,MyObject myObject){
      myParameter.setProperty(&quot;value&quot;);
      System.out.println(myObject.myFunction(myParameter));// outputs myResult
  
      myParameter.setProperty(&quot;otherValue&quot;);
      System.out.println(myObject.myFunction(myParameter));// outputs otherResult
  }
  </code></pre>
  <hr />
  <p>Here is the current solution, hopefully something better can be suggested.</p>
  <pre><code>private class MyObjectMatcher extends ArgumentMatcher&lt;MyObject&gt; {
  
      private final String compareValue;
  
      public ApplicationContextMatcher(String compareValue) {
          this.compareValue= compareValue;
      }
  
      @Override
      public boolean matches(Object argument) {
          MyObject item= (MyObject) argument;
          if(compareValue!= null){
              if (item != null) {
                  return compareValue.equals(item.getMyParameter());
              }
          }else {
              return item == null || item.getMyParameter() == null;
          }
          return false;
      }
  }
  
  public void initMock(MyObject myObject){
      MyObjectMatcher valueMatcher = new MyObjectMatcher(&quot;value&quot;);
      MyObjectMatcher otherValueMatcher = new MyObjectMatcher(&quot;otherValue&quot;);
      Mockito.when(myObject.myFunction(Matchers.argThat(valueMatcher))).thenReturn(&quot;myResult&quot;);
      Mockito.when(myObject.myFunction(Matchers.argThat(otherValueMatcher))).thenReturn(&quot;otherResult&quot;);
  }
  </code></pre>