我有这个类只是将http调用委托给第三方库:
public class HttpService
{
Gson gson = new Gson();
public <T> T fromJson(String json, Class<T> classOfT)
{
return gson.fromJson(json, classOfT);
}
}
我试图从json方法中删除它,就像这样:
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);
...将值null赋给obj。为什么是这样?我试图让exampleObject返回。我已经尝试了几种不同的Matcher方法将类参数发送到模拟的构造函数(即除了Matchers.eq()之外,我还尝试过Matchers.same()和其他一些)。任何见解都将不胜感激!