Mocking BLE特性使用MockRxAndroidBle写入
问题描述:
我在写一个JUnit测试,用于使用MockRxAndroidBle将数据写入BLE特性。使用DeviceBuilder
上的addService(UUID, List<BluetoothGattCharacteristic>)
方法将特征添加到模拟设备中。特性本身是使用CharacteristicsBuilder创建的。测试运行时,失败并显示消息java.lang.RuntimeException: Method setValue in android.bluetooth.BluetoothGattCharacteristic not mocked. See http://g.co/androidstudio/not-mocked for details."
Mocking BLE特性使用MockRxAndroidBle写入
引用的链接指示应在单元测试中使用默认值。
testOptions { unitTests.returnDefaultValues = true }
启用,允许试验,让过去的特性设置,但试图写入特性时失败。此时的例外是BleCharacteristicNotFoundException
。仔细检查后,RxBleDeviceServices
中的BluetoothGattService
实例似乎在通过UUID检索特征时返回null。调试器还显示该服务没有任何特性,列表为空。我相信null是这个内部列表的默认值。
有没有办法创建一个JUnit测试来模拟写入特性?
答
您需要使用Roboelectric运行测试。因此,将此添加到您的单元测试类定义中
@RunWith(RobolectricTestRunner.class)