【问题标题】:Context not associated with any application instrumentation testing?上下文不与任何应用程序检测测试相关联?
【发布时间】:2020-02-12 11:56:13
【问题描述】:

我正在编写用于检查我的 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】:

    根据 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.
    

    【讨论】:

      猜你喜欢
      • 2011-06-29
      • 2017-11-18
      • 2018-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      相关资源
      最近更新 更多