我想问一下关于具有BluetoothDevice
变量的类的单元测试。安卓单元测试:模拟BluetoothDevice并写入包裹
我的对象是一个简单的对象,它包含一些原始变量和一个BluetoothDevice变量。我的对象也是可以parcelable的。
最终,我的代码在移动设备上完全正常工作,但运行我的单元测试时出现奇怪的错误。
我嘲笑BluetoothDevice类在测试类,如下所示:
@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public class BluetoothDeviceTest {
@Mock
BluetoothDevice device1;
@Before
public void initMocks(){
MockitoAnnotations.initMocks(this);
when(device1.getAddress()).thenReturn("01:02:03:04:05:06");
when(device1.getName()).thenReturn("device767b1");
when(device1.getBondState()).thenReturn(BOND_NONE);
}
}
以我对象其他原语中使用:
我使用
out.writeParcelable(mBluetoothDevice,0);
内部@Override public void writeToParcel(Parcel out, int flags)
方法。我使用
mBluetoothDevice = in.readParcelable(BluetoothDevice.class.getClassLoader());
里面protected MyObject(Parcel in)
构造函数。
,测试我的对象实例的瓜分单元测试失败,异常 java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
。只有单元测试是奇怪的。
我怀疑这是因为我的模拟的BluetoothDevice
变量在包裹中比正常实例中的要短,并且包裹中数据字段的顺序变得混乱。
有没有人测试过一个嘲讽BluetoothDevice
,可以给我一些提示?谢谢