如何使用自定义类覆盖Robolectric标准阴影类?

时间:2016-08-22 09:28:18

标签: java android bluetooth robolectric nosuchmethoderror

我正在编写一个连接蓝牙低功耗设备的简单应用程序。连接到设备的代码在HandlerThread上,因此,为了使处理程序线程在单元测试中工作,我使用了robolectric

使用robolectric后,处理程序线程工作正常,但还有另一个问题。

Robolectric不允许我模拟BluetoothDevice类,因为robolectric ShadowBluetoothDevice不包含方法 connectGatt(...)。所以,在运行时我得到错误:

java.lang.NoSuchMethodError: android.bluetooth.BluetoothDevice.connectGatt(Landroid/content/Context;ZLandroid/bluetooth/BluetoothGattCallback;)Landroid/bluetooth/BluetoothGatt;

at com.example.BLEGattTest.setup(BLEGattTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:250)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:176)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:142)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

所以,我在想,如果我可以覆盖ShadowBluetoothDevice,我实际上可以让它工作吗?

我的代码:

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
    public class BLEGattTest {

        private BLEGattScanner scanner;

        private Customer customer;

        private BluetoothDevice device;

        @Before
        public void setup(){

            customer = new Customer();
            customer.setDevice(device);

            device = Mockito.mock(BluetoothDevice.class);
            when(device.connectGatt(any(Context.class), anyBoolean(), any(BluetoothGattCallback.class))).thenReturn(null);

            scanner = new BLEGattScanner(RuntimeEnvironment.application);
            scanner.start();
        }

        @After
        public void tearDown() throws InterruptedException {
            if(scanner != null){
                scanner.stopThread();
                scanner.join(1000);
            }
        }

        @Test
        public void testMocks(){
            Assert.assertEquals("asdf", device.getName());
        }
    }

1 个答案:

答案 0 :(得分:0)

您添加设备,之后,您将模拟它。这不起作用 - 您需要模拟它然后设置设备。

customer.setDevice(device)设备未定义。