使用Robolectric更改Android连接

时间:2014-12-01 14:09:43

标签: java android robolectric

我正在开发一个使用Robolectric进行单元测试的Android项目。

我无法禁用互联网连接。

我有一个方法,它使用HttpUrlConnection向HTTP服务器发送请求并从HTTP服务器获取响应:boolean sendRequest()。如果请求成功,则返回true。

private boolean sendRequest() throws Exception
{
    URL url = new URL("http://example.com");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection( );
    int statusCode = urlConnection.getResponseCode( );
    return statusCode == 200;
}

在帮助程序类中,我有两种方法:void setConnectivity(boolean enable)boolean checkConnectivity()

public static void setConnectivity( boolean enabled) throws Exception
{
    Context context = Robolectric.application.getApplicationContext();

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    Robolectric.shadowOf(wifiManager).setWifiEnabled(enabled);
    wifiManager = (WifiManager) Robolectric.application.getSystemService(Context.WIFI_SERVICE);
    Robolectric.shadowOf(wifiManager).setWifiEnabled(enabled);

    ConnectivityManager dataManager  = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Robolectric.shadowOf(dataManager).setBackgroundDataSetting(enabled);
    dataManager = (ConnectivityManager)Robolectric.application.getSystemService(Context.CONNECTIVITY_SERVICE);
    Robolectric.shadowOf(dataManager).setBackgroundDataSetting(enabled);

    Robolectric.shadowOf(dataManager.getActiveNetworkInfo()).setConnectionStatus(enabled);

    Intent connIntent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION);
    connIntent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, enabled);
    Robolectric.getShadowApplication().sendBroadcast(connIntent);

    ShadowSettings shadowSettings = new ShadowSettings();
    shadowSettings.setWifiOn(enabled);
    shadowSettings.setAirplaneMode(enabled);
}

public static boolean checkConnectivity() throws Exception
{
    Context context = Robolectric.application.getApplicationContext();

    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }

    NetworkInfo mobileNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }

    return false;
}

我已经打包了通过Robolectric禁用互联网连接的所有内容到setConnectivity()方法,当通过checkConnectivity()检查时,我得到了预期的返回值,但是sendRequest()总是返回是的,虽然我预计在禁用互联网连接时请求会失败。

@Test
public void testConnectivity() throws Exception
{
    setConnectivity(true);
    assertTrue(checkConnectivity());
    assertTrue(sendRequest());

    setConnectivity(false);
    assertFalse(checkConnectivity());
    assertFalse(sendRequest()); // Assertion failed
}

我是否错过了禁用互联网连接的Robolectric方法调用?或者也许我的整个方法都是错误的,实际的请求永远不会受到Robolectric设置的影响?

测试方法属于Robolectric测试运行的测试类。

1 个答案:

答案 0 :(得分:0)

由于我的问题没有得到任何答案,我无法在网上找到答案,我想这是不可能的。

我发现Stack Overflow的答案似乎解决了我想要实现的目标,虽然它不是Robolectric,而是一个模拟框架:
Answer to "Robolectric: simulate network error in test"