4

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.

  1. How can I test BluetoothLeService ?
  2. Can I write plain unit tests or do I need to write special androidTestss where the device is connected during the tests?
  3. How would this look when I have integration systems as a build environment?
2
  • Can you clarify what you mean by test. Within the bluetoothLeService class it deals with the gatt(callbacks) discovering services etc. So what is it you want to test? The characteristics or putting data in or getting it out? Aug 11, 2020 at 10:40
  • To use the functions of the service create rivate BluetoothLeService mBluetoothLeService; then call your functions. I do this via a device control activity Aug 11, 2020 at 10:42

1 Answer 1

1

You should have some tests which run when your device is actually connected to another bluetooth device.

  • To ensure that you've implemented the boilerplate code for communicating with a real bluetooth device correctly.
  • To ensure you're reading / writing the correct characteristics that are expected from the device.

Most of the code is boilerplate, and will be the same in any application which uses bluetoothle. The differences will be:

  • Services, Characteristics, and what / when you update or read information from the bluetooth device.

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.

1
  • 3
    How would one mock a real bluetooth device in a unit test?
    – Mavv3006
    Mar 16, 2021 at 7:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.