http呼び出しをサードパーティのライブラリに委任するだけのこのクラスがあります。
public class HttpService
{
Gson gson = new Gson();
public <T> T fromJson(String json, Class<T> classOfT)
{
return gson.fromJson(json, classOfT);
}
}
次のように、その fromJson メソッドをスタブしようとしています。
ExampleObject exampleObject = new ExampleObject("value1", "value2");
HttpService _testHttpService = Mockito.mock(HttpService.class);
Mockito.when(_testHttpService.fromJson(Matchers.anyString(), Matchers.eq(ExampleObject.class))).thenReturn(exampleObject);
モックされた HttpService オブジェクトから fromJson メソッドを呼び出すと、次のようになります...
ExampleObject obj = _testhttpService.fromJson("blahblahblah" , ExampleObject.class);
...obj に null の値が代入されます。どうしてこれなの?exampleObject を返そうとしています。クラス引数をモックコンストラクターに送信するために、いくつかの異なるMatcherメソッドを試しました(つまり、Matchers.eq()に加えて、Matchers.same()などをいくつか試しました)。どんな洞察もいただければ幸いです!