title: Unit Testing a BroadcastReceiver
body: <p>I've been trying to figure out how to unit test my BroadcastReceiver and I have looked at StackOverflow and other websites but I can't find the solution to my problem.</p>
  
  <p>In my mainActivity I have the following two functions:</p>
  
  <pre><code>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);
          }
      }
  };
  </code></pre>
  
  <p>The registerNetRcvr is called from within the onResume function (and equally I have an unregister called from onPause).</p>
  
  <p>As can be seen from the above, I have a function (handleBcastReceiver) that is called to handle the onReceive event and thus have another class that then has a variety of private functions which are called from this function.</p>
  
  <p>Just to slightly complicate matters... upon the onReceive function being called, as detailed above the 'handleBcastReceiver' function would then need to 'retreive' the correct data... and, as such would make a call to actually retrieve the appropriate system data as follows:</p>
  
  <pre><code>private static NetworkInfo getWiFiNetworkInfo(Context context) {
  
      ConnectivityManager connManager = (ConnectivityManager)
              context.getSystemService(Context.CONNECTIVITY_SERVICE);
      return connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  }
  </code></pre>
  
  <p>I believe that I should emulate the behaviour of the onReceive callback but also I guess emulate what would be returned from the system during the call to getWiFiNetworkInfo.</p>
  
  <p>My thinking is that I should be using RoboElectric to do this as it can shadow the BroadcastReceiver/ConnectivityManager. I've also looked at Mockito however I believe that RoboElectric is what I require for this but I accept that I could well be wrong. As far as I'm aware, RoboElectric allows me to emulate 'the Android system' and Mockito would allow me to mock my own classes.</p>
  
  <p>At present I just can't see how to test the BroadcastReceiver. Additionally I'm not clear upon whether I should be running the emulator to do this or simply 'run' my unit test as the 'shadow' should contain everything I need. Finally, if I was to shadow the BroadcastReceiver, how do I get WIFI to be 'enabled' or 'disabled' through this shadow process? In fact, should I be emulating a BroadcastReceiver or just the ConnectivityManager... I'm genuinely confused!</p>
  
  <p>What I have done so far is to create a BroadcastReceiverTest class within the test section of my app (not the 'androidTest' section). I can do normal simple unit tests on functions, I'm just stuck with how to emulate this 'system' behaviour. </p>
  
  <p>As always, any help is greatly appreciated.</p>