2016-08-22 4 views
1

私は、Bluetooth低エネルギーデバイスに接続する簡単なアプリケーションを作成しています。デバイスに接続するコードはHandlerThreadにあります。ハンドラスレッドをユニットテストで動作させるために、私はrobolectricを使用しました。Robolectricの標準シャドウクラスをカスタムクラスでオーバーライドする方法は?

robolectricを使用した後、ハンドラスレッドは完全に機能しますが、別の問題がありました。

robolectricはrobolectric ShadowBluetoothDeviceクラスには、 connectGatt(...)が含まれていないため、BluetoothDeviceクラスを模擬することはできません。

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時間も多くのことを試しました。そして、私は単純なことを逃しました。それは本当にばかげたものでした.. haha​​ とにかく!ありがとう.... @thst –

+0

あなたが気にしないなら、それを答えとして追加します:-) – thst

答えて

0

あなたは、デバイスを追加し、その後、あなたはそれを嘲笑。それはうまくいかない - それを嘲笑してデバイスを設定する必要がある。

customer.setDevice(device)にデバイスは定義されていません。

関連する問題