NetworkUtils { public static boolean isNetworkAvailable(Context context) { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); return activeNetworkInfo != null && activeNetworkInfo.isConnected(); } private  NetworkUtils(); static boolean isNetworkAvailable(Context context); static String getAbsoluteUrl(String url); static String getTwitterUrl(String username);  }
@Test public void should_return_true_if_network_is_available() { when(manager.getActiveNetworkInfo().isConnected()).thenReturn(true); boolean networkAvailable = NetworkUtils.isNetworkAvailable(context); assertThat(networkAvailable).isTrue(); }
@Test public void should_return_false_when_network_is_null_or_not_connected() { when(manager.getActiveNetworkInfo().isConnected()).thenReturn(false); boolean res1 = NetworkUtils.isNetworkAvailable(context); when(manager.getActiveNetworkInfo()).thenReturn(null); boolean res2 = NetworkUtils.isNetworkAvailable(context); assertThat(res1).isEqualTo(res2); assertThat(res1).isFalse(); }