我想知道编写检查网络连接相关功能的单元测试的最佳实践。
我有一个执行四个功能的网络实用程序类
这是我的网络实用工具类中的函数。
public static boolean isNetworkAvailable(@NonNull Context context) {
//check general connectivity
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr != null) {
// active connection
return conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isConnected();
}
// no connection
return false;
}
public static boolean isConnected(@NonNull Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities capabilities = conMgr.getNetworkCapabilities(conMgr.getActiveNetwork());
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
} else {
NetworkInfo info = conMgr.getNetworkInfo(conMgr.getActiveNetwork());
return (info != null && info.isConnected());
}
}
return false;
}
public static boolean isConnectedWifi(@NonNull Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities capabilities = conMgr.getNetworkCapabilities(conMgr.getActiveNetwork());
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI));
} else {
NetworkInfo info = conMgr.getNetworkInfo(conMgr.getActiveNetwork());
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
}
return false;
}
public static boolean isConnectedMobile(@NonNull Context context) {
ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities capabilities = conMgr.getNetworkCapabilities(conMgr.getActiveNetwork());
return capabilities != null && (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR));
} else {
NetworkInfo info = conMgr.getNetworkInfo(conMgr.getActiveNetwork());
return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}
}
return false;
}
我想为这些功能编写单元测试,但在操纵网络连接以产生要测试的各种结果(方案)时遇到了困难。到目前为止,我提出了两个测试,以检查在禁用和断开wifi连接后是否仍启用wifi。同样,请在禁用移动数据后检查是否启用了该数据。
@Test
public void isWifiEnabled() {
WifiManager wifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
wifiManager.disconnect();
assertFalse(NetworkUtils.isConnectedWifi(mContext));
}
@Test
public void isDataPlanEnabled() {
setMobileDataEnabled(mContext, false);
assertFalse(NetworkUtils.isConnectedMobile(mContext));
}
private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass;
try {
conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);
setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
} catch (Exception e) {
e.printStackTrace();
}
}
如何测试网络是否可用?我当时正在考虑以编程方式启用飞行模式,但是从4.2开始禁用该模式。
由Settings.System定义的某些设备设置现在是只读的。如果 您的应用尝试将更改写入定义于 Settings.System已经移到Settings.Global,写 在Android 4.2及更高版本上运行时,操作将静默失败。 即使您为android:targetSdkVersion和 android:minSdkVersion低于17,您的应用无法修改 运行时已移至Settings.Global的设置 Android 4.2及更高版本。
如何测试网络是否可用并强制进行单元测试的启用/禁用状态?还是应该忽略这些功能的单元测试?