Unit Testing BroadcastReceiver

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" ). , , .

, .

+4
1

, , ...:) -, , , .

, " ", , ConnectivityManager, , handleBcastReceiver. , , , "" , , . onReceive , , , "handleBcastReceiver", , , , , .

, . , , , . , , , , , .

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 23, manifest = "/src/main/AndroidManifest.xml")
public class NetworkStateHandlerUnitTest {

    private static String NETWORK_TYPE_HSDPA = "HSDPA";
    private ConnectivityManager connectivityManager;
    private ShadowConnectivityManager shadowConnectivityManager;
    private Context context;

    @Before
    public void setUp() {

    }

    @Test
    public void handleBroadcastReceiverWIFIConnected() {

        context = RuntimeEnvironment.application.getApplicationContext();
        connectivityManager = getConnectivityManager();
        shadowConnectivityManager = Shadows.shadowOf(connectivityManager);

        //Create shadow Network Info - Connected, WIFI, No Subtype, available, connected
        NetworkInfo networkInfoShadow = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, ConnectivityManager.TYPE_WIFI, 0, true, true);
        shadowConnectivityManager.setNetworkInfo(ConnectivityManager.TYPE_WIFI, networkInfoShadow);

        NetworkHandler.NetworkInfo networkInfoResponse;

        //Call the function under test
        networkInfoResponse = NetworkHandler.handleBcastReceiver(context);

        //Validate the response
        assertEquals(networkInfoResponse.getNetworkType(), ConnectivityManager.TYPE_WIFI);
    }

    private ConnectivityManager getConnectivityManager() {
        return (ConnectivityManager) RuntimeEnvironment.application.getSystemService(context.CONNECTIVITY_SERVICE);
    }
}

, , , , , , - , :)

, BroadcastReceiver... , ...

@Test
public void handleBrcastRcvrWifiConn() {

    MainActivity activity = Robolectric.setupActivity(MainActivity.class);
    TextView tvConnStatus = (TextView) activity.findViewById(R.id.tvConnectionState);


    context = RuntimeEnvironment.application.getApplicationContext();
    connectivityManager = getConnectivityManager();
    shadowConnManager = Shadows.shadowOf(connectivityManager);

    //Create shadow Network Info - Connected, WIFI, No Subtype, available, connected
    NetworkInfo netInfoShadow = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED,
            ConnectivityManager.TYPE_WIFI, 0, true, true);
    shadowConnManager.setNetworkInfo(ConnectivityManager.TYPE_WIFI, networkInfoShadow);


    //Trigger BroadcastReceiver
    RuntimeEnvironment.application.sendBroadcast(new Intent("android.net.wifi.STATE_CHANGE"));

    //Validate result by checking value of textView
    assertEquals("Connected", tvConnectionState.getText());

}
+2

Source: https://habr.com/ru/post/1666886/


All Articles