I tried to figure out how to unit test my BroadcastReceiver, and I looked at StackOverflow and other websites, but cannot find a solution to my problem.
In my mainActivity, I have the following two functions:
private void registerNetRcvr(){
if (!isRcvrRegistered) {
isRcvrRegistered = true;
registerReceiver(receiver, new IntentFilter("android.net.wifi.STATE_CHANGE"));
registerReceiver(receiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
}
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
NetworkHandler.NetworkInfo netInfo = NetworkHandler.handleBcastReceiver(context);
if (netInfo!=null){
handleInfoChange(netInfo);
} else {
handleInfoChange(null);
}
}
};
RegisterNetRcvr is called from within the onResume function (and equally I have an unregistered call from onPause).
As can be seen from the above, I have a function (handleBcastReceiver) that is called to handle the onReceive event and, therefore, has another class, which then has many private functions called from this function.
, ... onReceive, , "handleBcastReceiver" "" ... , , :
private static NetworkInfo getWiFiNetworkInfo(Context context) {
ConnectivityManager connManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
return connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
}
, onReceive, , , getWiFiNetworkInfo.
, RoboElectric , BroadcastReceiver/ConnectivityManager. Mockito, , RoboElectric - , , , . , RoboElectric " Android", Mockito .
, BroadcastReceiver. , , "" unit test, "" , . , BroadcastReceiver, WIFI "" "" ? , BroadcastReceiver ConnectivityManager... !
, BroadcastReceiverTest ( "androidTest" ). , , .
, .