Change Android connection with Robolectric

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()); // Assertion failed } 

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.

+5
source share
1 answer

Since my question does not receive any answers, and I cannot find answers on the Internet, I think this is simply impossible.

I found an answer to a stack overflow that seems to solve what I want to achieve, although this is not with Robolectric, but with a mocking wireframe:
The answer to "Robolectric: simulate a network error during testing"

0
source

Source: https://habr.com/ru/post/1208044/


All Articles