Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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 在类中测试序列化方法的正确方法是什么?_Java_Android_Unit Testing - Fatal编程技术网

Java 在类中测试序列化方法的正确方法是什么?

Java 在类中测试序列化方法的正确方法是什么?,java,android,unit-testing,Java,Android,Unit Testing,我在类中有这个方法,它在JSONObject中序列化类的属性,非常简单: public String serialize(){ JSONObject wifiToAddJson = null; try { wifiToAddJson = new JSONObject(); wifiToAddJson.put("wifi_ssid", wifi_ssid); wifiToAddJson.put("wifi_password", wif

我在类中有这个方法,它在JSONObject中序列化类的属性,非常简单:

public String serialize(){
    JSONObject wifiToAddJson = null;
    try {
        wifiToAddJson = new JSONObject();
        wifiToAddJson.put("wifi_ssid", wifi_ssid);
        wifiToAddJson.put("wifi_password", wifi_password);
        wifiToAddJson.put("latitude", wifi_LatLng.latitude);
        wifiToAddJson.put("longitude", wifi_LatLng.longitude);
        wifiToAddJson.put("name", name);
    } catch (JSONException e) {
        ACRA.getErrorReporter().handleSilentException(new MyJSONException(TAG + e.getMessage()));
    }
    return wifiToAddJson.toString();
}
我想通过询问所有字段的值来测试这个方法是否正确。 我想做一个这样的测试:

@Test
public void testSerialize(){
    String wifiSerialized = wiFiToAdd.serialize();

    try {
        JSONObject jsonObject = new JSONObject(wifiSerialized);

        String ssid = (String) jsonObject.get("wifi_ssid");
        String password = (String) jsonObject.get("wifi_password");
        String name = (String) jsonObject.get("name");
        String latitude = (String) jsonObject.get("latitude");
        String longitude = (String) jsonObject.get("longitude");

        assertEquals("WIFI_TEST", ssid);
        assertEquals("wifiTest", password);
        assertEquals("WiFi Pruebas", name);
        assertEquals("40.420088", latitude);
        assertEquals("-3.688810", longitude);

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
但问题来了:

java.lang.RuntimeException: Method put in org.json.JSONObject not mocked. See https://sites.google.com/a/android.com/tools/tech-docs/unit-testing-support for details.
    at org.json.JSONObject.put(JSONObject.java)
    at com.wiffinity.easyaccess.model.wifi.WiFiToAdd.serialize(WiFiToAdd.java:32)
    at com.wiffinity.easyaccess.model.wifi.WiFiToAddTest.testSerialize(WiFiToAddTest.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:79)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)


Process finished with exit code -1
谷歌文档说:

用于运行单元测试的android.jar文件不包含任何实际代码,这些代码由真实设备上的android系统映像提供。相反,所有方法都会抛出异常(默认情况下)。这是为了确保您的单元测试只测试您的代码,而不依赖于Android平台的任何特定行为(您没有显式地模拟这些行为,例如使用Mockito)。如果这证明有问题,可以将下面的代码段添加到build.gradle中以更改此行为:

android {
// ...

    testOptions { 
        unitTests.returnDefaultValues = true
    }
}

我不想改变我测试的全部行为。如何以干净的方式测试方法,并且不改变测试行为

您可以将
序列化
方法分为两部分:

public void serializeInto(JSONObject target) throws JSONException {
    wifiToAddJson.put("wifi_ssid", wifi_ssid);
    wifiToAddJson.put("wifi_password", wifi_password);
    wifiToAddJson.put("latitude", wifi_LatLng.latitude);
    wifiToAddJson.put("longitude", wifi_LatLng.longitude);
    wifiToAddJson.put("name", name);
}

public String serialize(){
    JSONObject wifiToAddJson = null;
    try {
        wifiToAddJson = new JSONObject();
        serializeInto(wifiToAddJson);
    } catch (JSONException e) {
        ACRA.getErrorReporter().handleSilentException(new MyJSONException(TAG + e.getMessage()));
    }
    return wifiToAddJson.toString();
}
然后用模拟的
JSONObject
测试第一个,用类的模拟测试第二个(只是验证调用了方法
serialize


我对这个答案并不完全满意,因为它无法达到100%的覆盖率,而且我觉得它会降低代码的可读性。

将此添加到build.gradle文件中:

testCompile 'org.json:json:20160810' // org.json is included with Android, but Android.jar can not be used from unit tests
它将使您的单元测试直接使用JSON库,而不是Android中包含的JSON库


有关更多详细信息,请参阅。

我不确定是否要在两个公共方法中更改一个“工作”公共方法来测试它。这当然是可能的。我会记住的。我想你的问题的答案是: