我尝试使用 Robolectric 测试使用蓝牙的应用程序。但是当我尝试获取 BluetoothManager 时,Roboletric 中总是返回 null。
以下是我如何尝试在 Robolectric 的类(class)中获取蓝牙管理器。
final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
感觉我在 Robolectric 设置中遗漏了一些简单的东西?以下是用于设置类的注释。
@Config(
sdk = Build.VERSION_CODES.KITKAT
, manifest = "src\\main\\AndroidManifest.xml"
)
@RunWith(RobolectricTestRunner.class)
最佳答案
更新
如果您使用Robolectric v3.1+,问题和答案就会过时
==========================
Robolectric 提供 lot of services from the box 。但是,BluetoothManager
不是其中的一部分。我认为这是因为在 API18 之前我们使用了 BluetoothAdapter.getDefaultAdapter()
。
看起来您正在瞄准最新的 API,而“旧”方式不是您的选择。作为测试的解决方法,您可以使用描述的方法 here .
我还创建了 issue在 Robolectric GitHub 页面上
关于bluetooth - Roboletric 返回 null 蓝牙管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715709/