Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 由于Android JUnit中的模拟方法调用而获取null_Java_Android_Unit Testing_Junit_Mockito - Fatal编程技术网

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