我正在构建一个应用程序,我们可以通过它使用ConnectivityManager
类检测互联网连接的可用性,但我不确定如何使用Mockito
. 请帮我编写以下代码的单元测试用例:
class ConnectivityMgr @Inject constructor(val context: Context) {
fun isConnectedOrConnecting(): Boolean {
val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connMgr.getNetworkCapabilities(connMgr.activeNetwork)?.run {
return hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || hasTransport(
NetworkCapabilities.TRANSPORT_CELLULAR
)
}
} else {
@Suppress("DEPRECATION")
connMgr.activeNetworkInfo?.let {
return when (it.type) {
ConnectivityManager.TYPE_WIFI -> true
ConnectivityManager.TYPE_MOBILE -> true
else -> false
}
}
}
return false
}
}