I am working on an Android project that gets a module with Robolectric.
I canβt disconnect my internet connection.
I have a method that uses HttpUrlConnection to send a request and receive a response from an HTTP server: boolean sendRequest() . And it returns true if the request was successful.
private boolean sendRequest() throws Exception { URL url = new URL("http://example.com"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection( ); int statusCode = urlConnection.getResponseCode( ); return statusCode == 200; }
In the helper class, I have two methods: void setConnectivity(boolean enable) and 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; }
I already collected everything I could find to disconnect the Internet connection via Robolectric, to the setConnectivity() method, and when checking through checkConnectivity() I get the expected return value, BUT sendRequest() always returns true, although I expect unsuccessful requests when disconnecting internet connection.
@Test public void testConnectivity() throws Exception { setConnectivity(true); assertTrue(checkConnectivity()); assertTrue(sendRequest()); setConnectivity(false); assertFalse(checkConnectivity()); assertFalse(sendRequest());
Am I missing the Robolectric method to disconnect my internet connection? Or maybe my whole approach is wrong and the actual requests are never affected by Robolectric settings?
The test method is in the test class with the Robolectric test runner.
source share