Я пишу простое приложение, которое подключается к низкоэнергетическому устройству Bluetooth. Код, который подключается к устройству, находится на HandlerThread, поэтому, чтобы обработать поток обработчиков в модульном тесте, я использовал robolectric.Как переопределить класс теневого класса 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());
}
}
Вы добавляете устройство, и после этого вы издеваетесь над ним. Это не сработает - вам нужно издеваться над этим, а затем установить устройство. На 'customer.setDevice (device)' 'устройство не определено. – thst
Спасибо !!! Я пробовал много вещей за 5 часов, прежде чем я разместил это .. и я пропустил простую вещь .. это было действительно глупо .. haha Anyways !! спасибо .... @thst –
Я добавлю его в качестве ответа, если вы не возражаете :-) – thst