Roboletric returns null 蓝牙管理器

Roboletric returns null Bluetooth Manager

我已经尝试使用 Robollectric 测试我的使用蓝牙的应用程序。但是当我尝试获取 BluetoothManager 时,我总是在 Roboletric 中返回 null。

以下是我如何尝试在 Robolectric 中的 class 中获取蓝牙管理器。

final BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

感觉我在 Robolectric 的设置中遗漏了一些简单的东西?以下是用于设置 class.

的注释
@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 而 "old" 方式不是您的选择。作为测试的解决方法,您可以使用 here.

中描述的方法

我还在 Robolectric GitHub 页面

上创建了 issue