title: Mockito - right way to test a method with params
body: <p>I need your assistance please:</p>
  
  <p>I have this method:</p>
  
  <pre><code>public class MyTestClass {
  
  protected void foo(JSONObject result, String text){
          String userId = result.optString("name");
          mApi.sendInfo(userId, text, mListener);
      }
  }
  </code></pre>
  
  <p>In Mockito I do:</p>
  
  <pre><code>@Test
  public void whenFooIsCalledThenSendInfoGetsCalled(){
      MyTestClass testClassSpy = spy(mMyTestClass);
      JSONObject jsonOb = mock(JSONObject.class);
      when(jsonOb.optString("name")).thenReturn("something");
  
      testClassSpy.foo(eq(jsonOb), anyString());
  
      ....
      some verification....
  </code></pre>
  
  <p>The problem is, the when the foo method gets called, JSONObject result is null.
  I can't seem to get this to work. I thought that if I mock the object and make it return a String once optString("name") is called, would solve this issue but it seems NPE is all I get.
  What am I doing wrong?</p>
  
  <p>Thanks you</p>