Amateur question, but I can't seem to find an answer...
I am writing unit tests using Mockito and I have a utils function (isOnline(context)
) which checks if I am offline before making a network call.
I am not trying to test that function, so I want to mock the result of that function and not run any of the code within it.
I've tried two of my usual approaches:
given(isOnline(context)).willReturn(true)
when(isOnline(application)).thenReturn(true)
But both seem to be running the isOnline
method.
fun isOnline(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
...
}
... and I am getting the error
java.lang.NullPointerException: null cannot be cast to non-null type
android.net.ConnectivityManager
at org.triggerise.base.utils.NetworkUtilsKt.isOnline(NetworkUtils.kt:14)
Any suggestions on how to just mock the result without running the contents of the method?