0

I am using Robolectric to test my Android app. The app uses the AndroidBeaconLibrary. When i use Activity activity = Robolectric.setupActivity(MainActivity.class); as a simple test, I get an error in the onBeaconServiceConnect() method:

@Override
    public void onBeaconServiceConnect() {
        beaconManager.setRangeNotifier(new RangeNotifier() {
            @Override
            public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
                if (beacons.size() > 0) {
                    final double distance = beacons.iterator().next().getDistance();
                    Log.i("X", "The beacon is about " + distance + " meters away.");
                    if (distance > 1.8 && !mAlertShown) {
                        mAlertShown = true;


     alertOpen();
                } else if (distance < 1) {
                    mAlertShown = false;
                    Toast.makeText(MainActivity.this, "alert is now reset", Toast.LENGTH_SHORT).show();
                }
            }
        }
    });
    try {
        Identifier i = Identifier.parse("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx");
        Identifier i2 = Identifier.parse("xxxx");
        beaconManager.startRangingBeaconsInRegion(new Region("rangingUniqueId", i, i2, null));
    } catch (RemoteException e) {
    }
}

The method fails after calling beaconManager.startRangingBeaconsInRegion(new Region("rangingUniqueId", i, i2, null)); at this.serviceMessenger.send(msg); in the BeaconManager.class. with a NullPointerException.

When I deploy the app on my phone, everything works just fine.

Jenkins
  • 121
  • 1
  • 11
  • Is your test calling `onBeaconServiceConnect` manually? Or is the method somehow getting called automatically? It would be helpful to see your Robolectric test method. – davidgyoung Jun 05 '15 at 12:14
  • The only line in my test class is Activity activity = Robolectric.setupActivity(MainActivity.class);. The onBeaconServiceConnect method is called during this setup activity process. – Jenkins Jun 08 '15 at 05:49
  • Is the `onBeaconServiceConnect ` call inside MainActivity.java or is it inside the `setupActivity` method? Understand that Robolectric does not provide a working implementation of the Android SDK, just stubs. Method calls often return null like in the case you describe. So if you have the call to `onBeaconServiceConnect` inside `setupActivity`, it probably should not be there. – davidgyoung Jun 08 '15 at 12:41
  • The `onBeaconServiceConnect` method is inside my `MainActivity.class` which implements the `BeaconConsumer` interface. I just followed a simple Robolectric example to setup/build an activity. I also tried to replace `setupActivity(MainActivity.class)` with `buildActivity(MainActivity.class).create().get()` which caused the same exception. Meanwhile, my other tests (web service, database, etc.) are all up and running. I'm just trying to understand where this NullPointerException comes from. Maybe it's because Robolectric is working with stubs only, like you said. – Jenkins Jun 10 '15 at 06:47

1 Answers1

0

I can't comment so im going to write it here as answer but this sound like in this.serviceMessenger.send(msg); the value of msg is NULL and that is why you get the exception.

public void startRangingBeaconsInRegion(Region region) throws RemoteException {
    Message msg = Message.obtain(null, IBeaconService.MSG_START_RANGING, 0, 0);
    StartRMData obj = new StartRMData(new RegionData(region), rangingCallbackAction());
    msg.obj = obj;
    msg.replyTo = rangingCallback;
    if(msg != null){
    serviceMessenger.send(msg);
    }
}
Maantje
  • 1,781
  • 2
  • 20
  • 33
  • Yes indeed. However, I don't know why the msg is null. It's inside the BeaconManager.class and everything works fine running the app on a real device. Why could it be null when I use Robolectric.setupActivity() instead to test it? – Jenkins Jun 05 '15 at 07:52
  • You can edit the IBeaconManager.class to not do that part if it is null ill edit my my answer with the code im not sure what the results will be ;) – Maantje Jun 05 '15 at 08:09
  • The BeaconManager.class comes from the AltBeacon Android Beacon Library dependency. Hence I cannot modify the .class file. – Jenkins Jun 05 '15 at 10:13
  • you can download the source from https://github.com/AltBeacon/android-beacon-library and then edit it. and then add it as i library but im not sure if that is worth it as it works on real devices you will have to decide that for yourself. – Maantje Jun 05 '15 at 14:59