2014-03-27 70 views
0

我使用JUnit測試我的android應用程序。這是我第一次測試JUnit,所以我不知道我應該遵循的路徑。現在我正在測試查詢到聯繫人列表。許多方法僅需要Raw Contact IDContact ID作爲參數,其中大多數方法或者返回JSON Object,JSON ArrayArrayListJUnit測試 - 最好和最可靠的方式來做我的測試?

我的問題是,我應該如何比較我的預期結果和實際結果?

現在我這樣的測試他們:

public void testGetRelationship() { 
    JSONArray jsonArrayActual = new JSONArray(); 
    JSONArray jsonArrayExpected = new JSONArray(); 

    try { 
     jsonArrayExpected = contact.getRelationship(RAW_CONTACT_ID); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     fail("Failed when calling the getRelationship method. " + e.getMessage()); 
    } 

    Cursor cursor = getContext().getContentResolver().query(Data.CONTENT_URI, 
      new String[]{Relation.NAME, Relation.TYPE, Relation._ID, Relation.LABEL}, 
      Relation.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", 
      new String[]{RAW_CONTACT_ID, Relation.CONTENT_ITEM_TYPE}, 
      null); 

    if(cursor.moveToFirst()) 
    { 
     do{ 
      try { 
       final JSONObject jsonRelationshipObject = new JSONObject(); 

       final String name = cursor.getString(0); 
       final Integer type = cursor.getInt(1); 
       final Integer id = cursor.getInt(2); 
       final String label = cursor.getString(3); 

       jsonRelationshipObject.put("id", id); 
       jsonRelationshipObject.put("type", type); 
       jsonRelationshipObject.put("label", label); 
       jsonRelationshipObject.put("name", name); 

       jsonArrayActual.put(jsonRelationshipObject); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
       fail("Failed while adding the values to the JSONObject. " + e.getMessage()); 
      }    
     } while(cursor.moveToNext()); 
    } 
    else 
    { 
     cursor.close(); 
     fail("RawContact not found! ID: " + RAW_CONTACT_ID); 
    } 

    cursor.close(); 

    try { 
     JSONAssert.assertEquals(jsonArrayExpected, jsonArrayActual, true); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     cursor.close(); 
     fail("JSONAssert exception: " + e.getMessage()); 
    } 

} 

基本上我打電話給我的真正的方法和幾乎重新編碼我(測試)方法。我發現這是無用的,或者至少非常無聊和乏味(再次編碼我之前編寫的代碼)。我很確定有更好的方法來執行JUnit測試。我想過我自己查詢聯繫人列表數據庫和手工建立我的對象進行比較,這樣的事情:

public void testGetRelationship() { 
    JSONArray jsonArrayActual = new JSONArray(); 
    JSONArray jsonArrayExpected = new JSONArray(); 

    try { 
     jsonArrayExpected = contact.getRelationship(RAW_CONTACT_ID); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     fail("Failed when calling the getRelationship method. " + e.getMessage()); 
    } 

    jsonRelationshipObject.put("id", 6); 
    jsonRelationshipObject.put("type", 1); 
    jsonRelationshipObject.put("label", "A label"); 
    jsonRelationshipObject.put("name", "the name"); 

    jsonArrayActual.put(jsonRelationshipObject); 

    try { 
     JSONAssert.assertEquals(jsonArrayExpected, jsonArrayActual, true); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     cursor.close(); 
     fail("JSONAssert exception: " + e.getMessage()); 
    } 
} 

這是更快的代碼,但拉回就是我需要手動從手機下載數據庫(未對我來說是一個大問題),並手動查詢它以獲取數據(不是很重要),但它不是非常通用的,如果數據庫發生變化,我猜所有的測試都會失敗。但是我可以獲取數據庫的快照並保存,然後始終在該快照上執行測試。

有什麼建議嗎?改進?

+0

什麼是junit版本? –

+0

@PeterRader我正在使用版本3. – dazito

回答

0

單元測試是沒有數據庫或外部服務器的低級測試。您搜索的是集成測試甚至點擊測試。

+0

我正在測試我開發的應用程序庫。在我進入用戶界面之前,我想測試它們以確保它們至少非常可靠。有什麼建議麼? – dazito

+0

請參閱:http://developer.android.com/tools/testing/testing_ui.html –

+0

據我所知,這主要是(如果不是全部)用戶界面測試。我的應用中沒有任何用戶界面。它們是使用RESTlet框架構建在REST服務器上的庫。我只想測試我的庫,而不是我的用戶界面(因爲我沒有用戶界面,應用程序啓動並啓動REST服務器,並使用PC上的瀏覽器發出請求,並等待來自我的庫的回覆) – dazito

1

你應該看看到的Mockito:https://code.google.com/p/mockito/

基本上,你要嘲笑你的數據源/服務接口光標/內容解析,並用硬編碼值(數據庫快照的一些樣本)實現它。 但是,只有當您必須維護查詢機制時才應該這樣做;如果這超出了您的項目範圍和/或沒有動手,這將不會有幫助。

對於您的testGetRelationship,這更多的是關於集成測試:您在管道的一側注入一些數據,並期望在另一側獲得相同的數據。

在這種情況下,JUnit可能不是正確的工具; JMeter應該更好地滿足這些需求。 下面是使用JMeter的測試JSON數據的一個例子: Jmeter extracting fields/parsing JSON response

添加您的「集成測試」在您的SDLC連接一個真實的數據源,但這些不應該運行的每個源碼編譯/開發週期。您只應在部署時運行「集成測試」來檢查端到端集成。