Utils { public static boolean isNetworkAvailable(Context context) { try { ConnectivityManager connManager = (ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE); if (connManager.getActiveNetworkInfo() != null && connManager.getActiveNetworkInfo() .isAvailable() && connManager.getActiveNetworkInfo().isConnected()) { return true; } } catch (Exception ex) { Logger.e(ex.getMessage()); return false; } return false; }  static String mappingToCreditCardAuthentication(String type, boolean secure); static boolean isNetworkAvailable(Context context); static void hideKeyboard(Context context, View editText); static String getFormattedAmount(double amount); static String getValidityTime(String transactionTime); static String getMonth(int monthValue); static int dpToPx(int dp); static String getFormattedCreditCardNumber(String unformattedCardNumber); static String getCardType(@NonNull String cardNo); static String getFormattedTime(long time); static String formatDouble(double d); static String getDeviceType(Activity activity); static final String CARD_TYPE_VISA; static final String CARD_TYPE_MASTERCARD; static final String CARD_TYPE_AMEX; static final String CARD_TYPE_JCB; }
@Test public void isNetworkAvailableTest_excption() { Mockito.when(conManagerMock.getActiveNetworkInfo()).thenReturn(null); Mockito.when(networkInfoMock.isAvailable()).thenReturn(true); Mockito.when(networkInfoMock.isConnected()).thenReturn(true); Assert.assertFalse(Utils.isNetworkAvailable(contextMock)); }
@Test public void isNetworkAvailableTest_notconnected() { Mockito.when(conManagerMock.getActiveNetworkInfo()).thenReturn(null); Mockito.when(networkInfoMock.isAvailable()).thenReturn(false); Mockito.when(networkInfoMock.isConnected()).thenReturn(true); Assert.assertFalse(Utils.isNetworkAvailable(contextMock)); }
@Test public void isNetworkAvailableTest_notAvailable() { Mockito.when(conManagerMock.getActiveNetworkInfo()).thenReturn(null); Mockito.when(networkInfoMock.isAvailable()).thenReturn(true); Mockito.when(networkInfoMock.isConnected()).thenReturn(false); Assert.assertFalse(Utils.isNetworkAvailable(contextMock)); }
@Test public void isNetworkAvailableTest() { Mockito.when(conManagerMock.getActiveNetworkInfo()).thenReturn(networkInfoMock); Mockito.when(networkInfoMock.isAvailable()).thenReturn(true); Mockito.when(networkInfoMock.isConnected()).thenReturn(true); Assert.assertTrue(Utils.isNetworkAvailable(contextMock)); }
@Test public void isNetworkAvailableTest_excptionError() { Mockito.when(contextMock.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(null); Mockito.when(conManagerMock.getActiveNetworkInfo()).thenReturn(null); Utils.isNetworkAvailable(null); PowerMockito.verifyStatic(Mockito.times(1)); Logger.e(Matchers.anyString()); }