上下文是否与任何应用程序测试都不相关?

问题描述 投票:0回答:1

我正在编写一种检查我的android设备状态的方法的工具测试用例。我就是这样嘲笑上下文:

mContext = InstrumentationRegistry.getInstrumentation().getContext();

我正在嘲弄此声明的上下文:

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

但是在达到此陈述后,它给出了以下例外:

 ----- 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 -----

[有人可以帮助我,如果我对它的嘲笑不正确或有什么问题吗?任何指导将不胜感激?

android testing mockito android-context android-instrumentation
1个回答
0
投票

根据Instrumentation.class文档。它说

 * 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

所以您为您的案例使用了错误的上下文。您需要设备的上下文,您可以按如下方式获得它。

mContext = InstrumentationRegistry.getInstrumentation().getContext();

并且根据文档,这将为您提供设备上应用程序的上下文。

 * 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.
© www.soinside.com 2019 - 2024. All rights reserved.