android - 你将如何测试这些功能?

标签 android unit-testing testing robotium testability

我是测试新手。当我开发我的应用程序时,我使用 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;
}

如何测试这些功能?

非常感谢!!

最佳答案

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

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

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

关于android - 你将如何测试这些功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24636021/

相关文章:

python - 在子文件夹中使用 pytest where test

c# - 是否需要对接口(interface)的定义进行单元测试?

java - 模拟类将 null 分配给 @NonNull 字段(Android)

javascript - 测试库错误 : mockConstructor(. ..):渲染没有返回任何内容

ruby-on-rails - 使用 rspec 测试该方法在另一个方法中被调用

java - 需要有关 android 中的 listitem click 的帮助

javascript - 带有颜色选择器 Eyecon 的 Android Webview

javascript - 远程运行 Cypress 测试运行程序

android - 移动应用程序中的客户端 SSL 证书有多安全?

android - BroadcastReceiver 没有收到警报的广播