mercredi 12 février 2020

Context not associated with any application instrumentation testing?

I am writing instrumentation test cases of a method that checks the state of my android device. I am mocking the context for this statement:

 BluetoothManager bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);

But upon reaching this statement it gives the following exception:

 ----- begin exception -----
E/TestRunner: java.lang.IllegalArgumentException: context not associated with any application (using a mock context?)
        at android.bluetooth.BluetoothManager.<init>(BluetoothManager.java:66)
        at android.app.SystemServiceRegistry$8.createService(SystemServiceRegistry.java:242)
        at android.app.SystemServiceRegistry$8.createService(SystemServiceRegistry.java:240)
        at android.app.SystemServiceRegistry$CachedServiceFetcher.getService(SystemServiceRegistry.java:997)
        at android.app.SystemServiceRegistry.getSystemService(SystemServiceRegistry.java:949)
        at android.app.ContextImpl.getSystemService(ContextImpl.java:1847)
        at com.resatech.android.scoutlib.uidata.ScoutDevice.init(ScoutDevice.java:251)
        at com.resatech.android.scoutlib.uidata.ScoutDevice.<init>(ScoutDevice.java:190)
        at com.resatech.android.scoutandroid.viewModels.ScoutDeviceSystemStatsViewModelTest.ScoutDeviceSystemStatsViewModelTest_ValuesUnknown(ScoutDeviceSystemStatsViewModelTest.java:74)
        at java.lang.reflect.Method.invoke(Native Method)
        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.InvokeMethod.evaluate(InvokeMethod.java:17)
        at androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        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.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runners.Suite.runChild(Suite.java:128)
        at org.junit.runners.Suite.runChild(Suite.java:27)
        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.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
        at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
        at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:392)
        at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2119)
    ----- end exception -----

Can someone help me and tell me if im mocking it incorrectly or what is the issue? Any guidance would be highly appreciated ?

Aucun commentaire:

Enregistrer un commentaire