Java 由于Android JUnit中的模拟方法调用而获取null
我试图模拟在另一个函数中调用的函数。但我得到的最终结果是Java 由于Android JUnit中的模拟方法调用而获取null,java,android,unit-testing,junit,mockito,Java,Android,Unit Testing,Junit,Mockito,我试图模拟在另一个函数中调用的函数。但我得到的最终结果是null。我试图模拟实际函数中使用的第二个函数 这是我的密码: @RunWith(MockitoJUnitRunner.class) public class LoadJsonData_Test { @Mock LoadJsonData loadJsonData; @Test public void getChartTypeJS_test() { String jsonStr = ""; try { I
null
。我试图模拟实际函数中使用的第二个函数
这是我的密码:
@RunWith(MockitoJUnitRunner.class)
public class LoadJsonData_Test {
@Mock
LoadJsonData loadJsonData;
@Test
public void getChartTypeJS_test() {
String jsonStr = "";
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("chartInfo.json");
int size = is.available();
byte[] buffer = new byte[size];
if (is.read(buffer) > 0)
jsonStr = new String(buffer, "UTF-8");
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
when(loadJsonData.getJsonData()).thenReturn(jsonStr);
System.out.println(loadJsonData.getJsonData()); //Printing the data I wanted
assertEquals(loadJsonData.getChartTypeJS(),
"javascript:setChartSeriesType(%d);"); // loadJsonData.getChartTypeJS() returns null
}
我正在尝试测试的代码:
public String getJsonData() {
try {
InputStream is = mContext.getAssets().open("chartInfo.json");
int size = is.available();
byte[] buffer = new byte[size];
if (is.read(buffer) > 0)
jsonString = new String(buffer, "UTF-8");
is.close();
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return jsonString;
}
public String getChartTypeJS() {
jsonString = getJsonData();
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONObject javascriptEvent_JsonObject = jsonObject.getJSONObject("javascript_events");
return javascriptEvent_JsonObject.getString("chartType");
} catch (JSONException e) {
e.printStackTrace();
}
return "";
}
我做错了什么
谢谢您正在模拟
LoadJsonData
,然后在其上调用两个方法:
getJsonData()
getChartTypeJS()
getJsonData()
中创建对响应的期望:
when(loadJsonData.getJsonData()).thenReturn(jsonStr);
但是由于mock不期望来自getChartTypeJS()
的响应,因此该调用返回null:loadJsonData.getChartTypeJS()
看起来LoadJsonData
应该是一个Spy
而不是Mock
,因为这将允许您模拟getJsonData()
,但调用getChartTypeJS()
的实际实现
例如:
@Spy
LoadJsonData loadJsonData;
// this wil tell Mockito to return jsonStr when getJsonData() is invoked on the spy
doReturn(jsonStr).when(loadJsonData.getJsonData());
// this will invoke the actual implementation
assertEquals(loadJsonData.getChartTypeJS(), "javascript:setChartSeriesType(%d);");
有关间谍活动(也称为部分模拟)的更多详细信息。您正在模拟
LoadJsonData
,然后在其上调用两个方法:
getJsonData()
getChartTypeJS()
getJsonData()
中创建对响应的期望:
when(loadJsonData.getJsonData()).thenReturn(jsonStr);
但是由于mock不期望来自getChartTypeJS()
的响应,因此该调用返回null:loadJsonData.getChartTypeJS()
看起来LoadJsonData
应该是一个Spy
而不是Mock
,因为这将允许您模拟getJsonData()
,但调用getChartTypeJS()
的实际实现
例如:
@Spy
LoadJsonData loadJsonData;
// this wil tell Mockito to return jsonStr when getJsonData() is invoked on the spy
doReturn(jsonStr).when(loadJsonData.getJsonData());
// this will invoke the actual implementation
assertEquals(loadJsonData.getChartTypeJS(), "javascript:setChartSeriesType(%d);");
有关间谍活动的更多详细信息(也称为部分模拟)。从何处获取空值?我看到了几种方法。你期待什么呢?因为嘲弄会回来nulls@AntonKazakov非常感谢。你是对的。你从哪里得到空值?我看到了几种方法。你期待什么呢?因为嘲弄会回来nulls@AntonKazakov非常感谢。你说得对,这对我来说很有意义。非常感谢。但是由于JSONObject是android.jar的一部分,我们不能使用JSONObject调用。那么我应该使用RoboeElectric还是你建议的@glytchingI?我不确定最后的评论指的是什么。这可能是一个错误吗?我认为这是一个错误:java.lang.RuntimeException:org.json.JSONObject中的getJSONObject方法未被模拟。有关详细信息,请参阅@我认为这是一个问题。请参考此链接:@glytchingThat对我来说现在有意义了。非常感谢。但是由于JSONObject是android.jar的一部分,我们不能使用JSONObject调用。那么我应该使用RoboeElectric还是你建议的@glytchingI?我不确定最后的评论指的是什么。这可能是一个错误吗?我认为这是一个错误:java.lang.RuntimeException:org.json.JSONObject中的getJSONObject方法未被模拟。有关详细信息,请参阅@glytchingI认为这是一个问题。请参考此链接:@glytching