Reputation: 53
In my android app, there the following code:
BluetoothManager bm = (BluetoothManager)activity.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter ba = bm.getAdapter();
I want to do Unit Tests using Robolectric. I am using Robolectric 2.2. But I found that bm is null, and I don't know how to mock it.
Upvotes: 4
Views: 1395
Reputation: 17495
The challenge with testing for bluetooth and other system services is that these classes are all final.
So if you want to inject anything else you need to make a wrapper around the final class and then provide an implementation forwarding to actual bluetooth classes and an alternative one that mocks your stuff. But then all coded needs to be updated to use that wrapper instead of the final classes directly.
PowerMock however should be able to handle these final classes. The summary (follow link for more detail)
Upvotes: 1