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

我该如何测试这些功能?

非常感谢!!

4

1 回答 1

2

对于第一个,您应该存根/模拟连接管理器。

模拟连接管理器可能产生的所有条件,并确保您的方法为每个条件返回正确的值。else if您可能只想返回值,而不是嵌套语句。IMO 这使代码更清晰,更容易思考。

对于第二个,我什至不确定我是否会打扰,因为您基本上是在确保 Android 调用适用于您提供的值——您需要解释您想要测试的具体内容。那个反射有效吗?你用合适的尺寸称呼它吗?

于 2014-07-08T15:47:40.943 回答