Unit Testing Estimote

How can we perform junit unit testing for onEnteredRegion and onExitedRegion when we start monitoring for beacons?

Hi @yrvlucero, that’s true that we do not provide MockBeaconManager or something like that.

This is what we do internally. Class that is using BeaconManager’s callbacks onEnteredRegion and onExitedRegion is dispatching those calls to protected methods of that class. In your unit test you can mock responses by invoking those methods directly with self constructed Beacon objects.

Something between lines:

manager.setMonitoringListener(region, beacons ->
  doSomething(region, beacons)
)

in test

   object = new ObjectThatDoesMonitoring();
   object.doSomething(..., ...)
1 Like

How can I make the testcase for this sample code.

BeaconManager beaconManager = new BeaconManager(context);
beaconManager.setMonitoringListener(new MonitoringListener() {
    // ... close to us.
    @Override
    public void onEnteredRegion(Region region, List<Beacon> beacons) {
        mBeacons = beacons;
    }

    // ... far away from us.
    @Override
    public void onExitedRegion(Region region) {
    }
});

// Connect to the beacon manager...
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
    @Override
    public void onServiceReady() {
        try {
            // ... and start the monitoring
            beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS_REGION);
        } catch (Exception e) {
        }
    }
});

You can safely skip unit testing of connecting to BeaconManager. Just set from test mBeacons instance variable and do your logic later.

1 Like