安卓单元测试

3

我正试图为我的一个类编写一个AndroidTestCase,该类连接到服务器并解析返回的JSONObject。当我在UI中测试功能时,文件可以正常工作并解析和显示正确的信息。当我将URL输入到浏览器中时,我会收到正确的JSONObject回复。然而,当我尝试通过AndroidTestCase获取JSONObject并仅验证它不为空时,它会在尝试获取对应于URL的JSONObject时抛出IOException异常。我已验证它正在使用的URL是正确的。以下是堆栈跟踪。

java.net.UnknownHostException: api.penncoursereview.com
 at java.net.InetAddress.lookupHostByName(InetAddress.java:506)
 at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
 at java.net.InetAddress.getAllByName(InetAddress.java:256)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
 at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
 at edu.upenn.cis.cis350.backend.Parser.retrieveJSONObject(Parser.java:34)
 at edu.upenn.cis.cis350.test.ParserTest.test_retrieveJSONObject(ParserTest.java:22)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at junit.framework.TestCase.runTest(TestCase.java:154)
 at junit.framework.TestCase.runBare(TestCase.java:127)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
 at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
 at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
 at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

不知道为什么代码在模拟器中能够运行,但在测试中不行?

提前感谢您的帮助!

编辑:

以下是相关方法:

public JSONObject retrieveJSONObject(String path){
    try{
        URL url = new URL(path);
        Log.w("Parser: retrieveJSONObject", "url=" + url);
        URLConnection connection = url.openConnection();
        String line;
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        while ((line = reader.readLine()) != null) {
            builder.append(line);
        }
        Log.v("Length",builder.toString());

        return new JSONObject(builder.toString());
    }
    catch(IOException e) {
        Log.w("Parser: retrieveJSONObject", "IOException: Bad Url");
        e.printStackTrace();
        return null;
    } catch (JSONException e) {
        Log.w("Parser: retrieveJSONObject", "JSONException: mis-formatted JSON");
        e.printStackTrace();
        return null;
    }
}

第34行是初始化BufferedReader的行。

你能提供一些解析器类的相关代码吗(大约在第34行左右)? - 207
也许你的测试用例方法不仅仅是调用“retrieveJSONObject”。如果是这样,请贴出那部分代码。 - 207
我在上面添加了retrieveJSONObject方法。我认为这个方法相当独立,可以单独测试而不影响其他方法。 - Jin
没问题。奇怪。Mh UnknownHostException通常是没有连接到互联网时出现的。请确保您的测试设备(或模拟器)已连接。 - 207
1个回答

1
我猜你缺少的是INTERNET权限。 考虑到你的方法在Utils类中被定义为static,下面的测试工作正常。
public void testRetrieveJSONObjectWithUrl() {
    final String url = "http://www.bom.gov.au/fwo/IDV60901/IDV60901.94868.json";
    assertNotNull(Utils.retrieveJSONObject(url));
}

啊,我没有在我的测试项目中添加网络权限。非常感谢! - Jin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接