2015-09-26 76 views
1

我已經嘗試運行以下JUnit測試。當我嘗試打印出userHash字符串時,我得到該字符串爲空。這表明httpclient根本沒有運行。我一直在關注這個帖子...使用JUNIT測試運行AsyncHttpClient

https://github.com/loopj/android-async-http/issues/173

這裏是我的課。

public class ECApiManagerTests { 
    InstrumentationTestCase runnerHelper = new InstrumentationTestCase(); 
    private final String userEmail = "removed"; 
    private final String passWord = "removed"; 
    String userHash; 


    @Test 
    public void testPOST()throws Throwable{ 
     final AsyncHttpClient httpClient = new AsyncHttpClient(); 


     runnerHelper.runTestOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       RequestParams params = new RequestParams(); 
       params.put("email", userEmail); 
       params.put("password", passWord); 
       httpClient.post("https://edu.chat/api/login/", params, new AsyncHttpResponseHandler() { 
        @Override 
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
         //called when response code 200 
         userHash = new String(responseBody); 
        } 
        @Override 
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 

        } 
       }); 


      } 
     }); 

     try { 
      System.out.println(userHash); 
      JSONObject obj = new JSONObject(userHash); 
      Assert.assertEquals("true", obj.getString("success")); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 





    } 

回答

0

您忘記了CountdownLatch解決方案的一部分。這很重要,因爲請求是異步的,所以您需要等到請求完成後再查看結果。鎖存器通過讓調用線程等待,直到從響應處理程序調用onFinish

public class ECApiManagerTests { 
    InstrumentationTestCase runnerHelper = new InstrumentationTestCase(); 
    private final String userEmail = "removed"; 
    private final String passWord = "removed"; 
    String userHash; 

    @Test 
    public void testPOST()throws Throwable{ 
     final AsyncHttpClient httpClient = new AsyncHttpClient(); 
     final CountDownLatch signal = new CountDownLatch(1); 

     runnerHelper.runTestOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       RequestParams params = new RequestParams(); 
       params.put("email", userEmail); 
       params.put("password", passWord); 
       httpClient.post("https://edu.chat/api/login/", params, new AsyncHttpResponseHandler() { 
        @Override 
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
         //called when response code 200 
         userHash = new String(responseBody); 
        } 
        @Override 
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 

        } 
        @Override 
        public void onFinish() { 
         signal.countDown(); 
        } 
       }); 


      } 
     }); 
     try { 
      signal.await(30, TimeUnit.SECONDS); // wait for callback 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     try { 
      System.out.println(userHash); 
      JSONObject obj = new JSONObject(userHash); 
      Assert.assertEquals("true", obj.getString("success")); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     }  
    } 
+0

感謝這一點,但由於某種原因字符串仍爲空... –

+0

您是否添加日誌記錄以查看您的'onFailure'是否被調用。如果是這樣,'userHash'將會是'null'。 – iagreen

+0

是的。它似乎從來沒有被稱爲..你知道這是爲什麼嗎? –