3

I am trying to write a AndroidTestCase for one of my classes that make connection to a server and parse the returned JSONObject. When I test the functionality in the UI, the file works fine and the correct information are parsed and displayed. When I input the URL into my browser, I get the correct JSONObject back. However, when I try to get the JSONObject through a AndroidTestCase and simply verifying that it's not null, I get IOException when it tries to get the corresponding JSONObject for an url. I verified that the url it's using is correct. Here's the stack trace.

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)

Any idea why the code works in simulator but not in test?

Thanks in advance for the help!

edit:

Here is the relevant method:

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;
    }
}

Line 34 is the line initializing the BufferedReader.

4
  • Can you supply some relevant code from your parser class (around line 34) Commented Mar 17, 2012 at 22:53
  • and maybe you do more than just calling "retrieveJSONObject" in your test case method. if so paste that code too Commented Mar 17, 2012 at 23:13
  • I added retrieveJSONObject method above. I think the method is pretty standalone and can be tested independently from others.. Commented Mar 18, 2012 at 2:37
  • Yes no problem with that. Strange. Mh UnknownHostException is typically when not connected to internet. Be sure your test-device (or emulator) is connected Commented Mar 18, 2012 at 11:08

1 Answer 1

1

I guess what you are missing is the INTERNET permission. Considering that your method is defined static in Utils class, the following test works.

public void testRetrieveJSONObjectWithUrl() {
    final String url = "http://www.bom.gov.au/fwo/IDV60901/IDV60901.94868.json";
    assertNotNull(Utils.retrieveJSONObject(url));
}
Sign up to request clarification or add additional context in comments.

1 Comment

Ah I didn't add internet permission to my test project. Thanks so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.