2014-07-08 136 views
0

我是測試新手。當我開發我的應用程序時,我使用Robotium來測試我的應用程序,但現在,我想測試一些屬於我的Util類的成員函數。例如:你會如何測試這些功能?

public static boolean internetConnection(Context context) { 
    ConnectivityManager conMgr = (ConnectivityManager) context 
      .getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo i = conMgr.getActiveNetworkInfo(); 
    if (i == null) 
     return false; 
    else if (!i.isConnected()) 
     return false; 
    else if (!i.isAvailable()) 
     return false; 

    return true; 
} 

,或者例如:

public static boolean isTabletDevice(Context context) { 
    if (android.os.Build.VERSION.SDK_INT >= 11) { // honeycomb 
     // test screen size, use reflection because isLayoutSizeAtLeast is 
     // only available since 11 
     Configuration con = context.getResources().getConfiguration(); 
     try { 
      Method mIsLayoutSizeAtLeast = con.getClass().getMethod(
        "isLayoutSizeAtLeast", int.class); 
      Boolean r = (Boolean) mIsLayoutSizeAtLeast.invoke(con, 
        0x00000004); // Configuration.SCREENLAYOUT_SIZE_XLARGE 
      return r; 
     } catch (Exception x) { 
      x.printStackTrace(); 
      return false; 
     } 
    } 
    return false; 
} 

我怎麼能測試這些功能呢?

非常感謝!

+0

有很多簡單的方法來確定設備是否是平板電腦。請參閱http://stackoverflow.com/questions/5832368/tablet-or-phone-android –

回答

2

對於第一個應該存根/模擬連接管理器。

模擬連接管理器可能產生的所有條件,並確保您的方法爲每個返回適當的值。您可能只想返回值,而不是嵌套else if語句。海事組織這使得代碼更清潔,更容易思考。

對於第二個我甚至不確定我是否打擾,因爲您基本上確保Android調用適用於您提供的值–,您需要解釋您要測試的具體內容。這種反思有效嗎?你用合適的尺寸調用它?