Open kmenager opened 8 years ago
+1 I have exact the same problem, The same NullPointer and can't find a work around.
Just so I understand the problem... The issue is that you want to use Robolectric in your app that uses the Android Beacon Library, but using RegionBootstrap
in the onCreate
method of your Application
class prevents you from running tests because you get this exception, yes?
In the case highlighted here, this line of code throws a NullPointerException because in the Robolectric environment there is no way to send a message using the Android APIs: https://github.com/AltBeacon/android-beacon-library/blob/2.8.1/src/main/java/org/altbeacon/beacon/BeaconManager.java#L534
This isn't a super easy problem to solve. Understand that Robolectric is used by this library for internal unit tests allows a way to run tests without access to an Android device, provided the tests are designed to handle shortcomings like these. It isn't really designed to make itself easily testable with Robolectic, doing so would require one of two things:
If any of you are interested in working on this, I can provide a few thoughts on how it might be put together.
David thanks for the the explanation. If you decide to keep this issue open, and you had time - drop your thoughts I will check if I'm able to come up with something valuable.
@speedingdeer, my basic thought would be to:
RobolectricBeaconManager
class that extends BeaconManager
but stubs all the public methods so they do nothing.BeaconManager.getInstanceForApplication
method to construct a RobolectricBeaconManager
instead of a regular BeaconManager
if a Robolectric context is active. This context could either be detected like described here or configured through a static method like BeaconManager.setRobolectricContext(true);
I'm sure there would be complications, but a first cut at trying to build this probably wouldn't be so hard.
thanks @davidgyoung, plan looks good.
The issue is that you want to use Robolectric in your app that uses the Android Beacon Library, but using RegionBootstrap in the onCreate method of your Application class prevents you from running tests because you get this exception, yes?
Yes It's exactly that !
Thanks @davidgyoung, I will try to mock BeaconManager. Hope someone found a solution to this problem
Hey @kmenager, I am having the same issue but I can't figure out how to mock BeaconManager. Have you had any luck doing this?
I have implemented your library with background support as described in your sample. In my Application class, I get an instance of BeaconManager, I create a RegionBootstrap and set background scan period.
I try to implement test in my app using Roboelectric.
I've got the following error :
I follow the response from @davidgyoung http://stackoverflow.com/questions/30499138/servicenotdeclaredexception-the-beaconservice-is-not-properly-declared-in-andro and add the line before BeaconManager.getInstance()
Know I have another error from RegionBootstrap
I think the problem come from this line https://github.com/AltBeacon/android-beacon-library/blob/master/src/main/java/org/altbeacon/beacon/BeaconManager.java#L308
Mobile device model and OS version
Nexus 5, Android 6.0.1
Android Beacon Library version
Version 2.8.1
Robolectric version
Version 3.1