Reputation: 3270
I am learning to communicate over Bluetooth Low Energy on Android.. Here is an example app
There in there source code are several Bluetooth related objects, which were final classes obviously:
private BluetoothManager mBluetoothManager;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothGatt mBluetoothGatt;
Of course I do not want to test library stuff like BluetoothManager, BluetoothAdapter or BluetoothGatt itself. But I want to test BluetoothLeService : Service
which was written in that project.
I do not know, how to mock these final BluetoothManager, BluetoothAdapter or BluetoothGatt
objects.
BluetoothLeService
?test
s or do I need to write special androidTests
s where the device is connected during the tests?Upvotes: 4
Views: 1863
Reputation: 21025
You should have some tests which run when your device is actually connected to another bluetooth device.
Most of the code is boilerplate, and will be the same in any application which uses bluetoothle. The differences will be:
So I would split your testing into two parts:
Code that simply connects to a bluetooth device, and reads and writes from the device. This module of code should be reusable in any app, and should not contain any business logic unique to your app. That way you need to test with a real device only when you make changes to this module of code.
Code that is unique to your app. This includes all the business logic that executes once you read / write information from the bluetooth device. Here you can write a mock class that pretends to be the bluetooth device from which you're reading / writing information. It can act as a layer between the business logic unique to your app, and the code which actually connects and interacts with the bluetooth device. During your automated tests, it pretends to provide mock data its read from the bluetooth device. When you want to test with a real device, toggle a flag, and have it read and write to a real bluetooth device using the code I mentioned in the previous bullet point.
Most of the code in the sample application is boiler plate. You should cut out anything extraneous from it that your app does not need and use it as a layer in your app that your business logic communicates with.
Upvotes: 1