2016-08-22 74 views
1

我正在寫一個連接到藍牙低功耗設備的簡單應用程序。連接到設備的代碼位於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()); 
     } 
    } 
+0

您添加設備,之後,你嘲笑它。這不能工作 - 你需要嘲笑它,然後設置設備。在'customer.setDevice(device)''device'是未定義的。 – thst

+0

謝謝!我嘗試了很多東西5小時我貼這個..和我錯過了一個簡單的事情..這是非常愚蠢的前...哈哈 反正!謝謝.... @thst –

+0

我會將它添加爲答案然後如果你不介意:-) – thst

回答

0

您添加的設備,並在這之後,你嘲笑它。這不能工作 - 你需要嘲笑它,然後設置設備。

customer.setDevice(device)該設備未定義。