I am writing instrumentation test cases of a method that checks the state of my android device. This is how im mocking the context:
mContext = InstrumentationRegistry.getInstrumentation().getContext();
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 ?
According to the Instrumentation.class documentation. It says
* Return the Context of this instrumentation's package. Note that this is
* often different than the Context of the application being
* instrumentated, since the instrumentation code often lives is a
* different package than that of the application it is running against.
* See {@link #getTargetContext} to retrieve a Context for the target
* application.
*
* @return The instrumentation's package context.
*
* @see #getTargetContext
So You are using the wrong context for your case. You need the device's context which u can get as follows.
mContext = InstrumentationRegistry.getInstrumentation().getContext();
And According to the documentation, this gives you the context of your application on your device.
* Return a Context for the target application being instrumented. Note
* that this is often different than the Context of the instrumentation
* code, since the instrumentation code often lives is a different package
* than that of the application it is running against. See
* {@link #getContext} to retrieve a Context for the instrumentation code.
*
* @return A Context in the target application.