I'm writing a JUnit test for writing data to a BLE characteristic using the MockRxAndroidBle. The characteristics are being added to the mock device using the addService(UUID, List<BluetoothGattCharacteristic>)
method on the DeviceBuilder
. The characteristics themselves are being created using the CharacteristicBuilder. When the test is run, it fails with the message java.lang.RuntimeException: Method setValue in android.bluetooth.BluetoothGattCharacteristic not mocked. See http://g.co/androidstudio/not-mocked for details."
The referenced link indicates that default values should be used in unit tests.
testOptions {
unitTests.returnDefaultValues = true
}
Enabling that allows the test to get past in setup of the characteristic but the fails when attempting to write the characteristic. The exception at this point is a BleCharacteristicNotFoundException
. Upon closer inspection it seems that the BluetoothGattService
instance in RxBleDeviceServices
returns null when retrieving the characteristic by UUID. The debugger also shows that the service does not have any characteristics and the list is null. I believe that null is the default value for this internal list.
Is there a way to create a JUnit test that mocks writing to a characteristic?
You need to run the test with Roboelectric. So add this to your unit test class definition
@RunWith(RobolectricTestRunner.class)