gpt4 book ai didi

java - Robolectric 未正确处理 getApplicationContext() - NPE

转载 作者:太空宇宙 更新时间:2023-11-04 10:46:15 25 4
gpt4 key购买 nike

我一直在使用 Robolectric 在 Android 应用程序上运行一些测试,到目前为止一直运行良好。

我的应用程序将 getApplicationContext() 的结果传递给构造函数,但 Robolectric 应该确保该值不为 null,因为我正在使用 BuildActivity() 方法。该错误出在我的测试环境的某些配置中,或者是 Robolectric 中的错误。结果是 Toast 构造函数中出现 NullPointerException。

我的环境:

com.android.tools.build:gradle:3.0.1
org.robolectric:robolectric:3.4.2
junit:junit:4.12

我正在尝试测试我们的 SystemMonitorActivity 类,它是抽象的

public abstract class SystemMonitorActivity extends BaseActivity implements OnClickListener

基本 Activity :

public class BaseActivity extends Activity implements IHandleMessageReceived, Thread.UncaughtExceptionHandler

我创建了 SystemMonitorActivity 的子类,以便我可以在测试中实例化它:

public class SystemMonitorActivitySubclass extends SystemMonitorActivity

这是我的模拟测试类,用于重现我所看到的错误:

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class BuildActivityTest {

@Test
public void onCreateTest000() throws Exception {
SystemMonitorActivitySubclass systemMonitorActivitySubclass = Robolectric.buildActivity(SystemMonitorActivitySubclass.class).create().get();
}

}

我的测试输出:

java.lang.NullPointerException
at android.widget.Toast.__constructor__(Toast.java:114)
at android.widget.Toast.<init>(Toast.java)
at org.robolectric.shadows.ShadowToast.makeText(ShadowToast.java:38)
at android.widget.Toast.makeText(Toast.java)
at com.siemens.hc.poc.isf.admin.sysmon.SystemMonitorActivity.setUpDeviceOwner(SystemMonitorActivity.java:649)
at com.siemens.hc.poc.isf.admin.sysmon.SystemMonitorActivity.onCreate(SystemMonitorActivity.java:401)
at android.app.Activity.performCreate(Activity.java:6975)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)
at org.robolectric.android.controller.ActivityController$1.run(ActivityController.java:73)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:366)
at org.robolectric.shadows.CoreShadowsAdapter$1.runPaused(CoreShadowsAdapter.java:27)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:70)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:80)
at com.siemens.hc.poc.isf.admin.sysmon.BuildActivityTest.setUp(BuildActivityTest.java:63)
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.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:228)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:110)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:37)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:64)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy1.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:108)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:146)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:128)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)

我已确保在 build.gradle 的测试中包含 Android 资源:

android.testOptions.unitTests.includeAndroidResources = true

我是否需要以某种方式将 SystemMonitorActivitySubclass 添加到 AndroidManifest.xml 中?

<小时/>

通过堆栈跟踪,实际的 NPE 出现在以下代码 fragment 中:

java.lang.NullPointerException
at android.widget.Toast.__constructor__(Toast.java:114)

public Toast(@NonNull Context context, @Nullable Looper looper) {
mContext = context;
mTN = new TN(context.getPackageName(), looper);
...

这是从 Robolectrics ShadowToast 调用的:

at android.widget.Toast.<init>(Toast.java)
at org.robolectric.shadows.ShadowToast.makeText(ShadowToast.java:38)

@Implementation
public static Toast makeText(Context context, CharSequence text, int duration) {
Toast toast = new Toast(context);
...

传递给 makeText 的上下文是 getApplicationContext(),它应该由 Robolectric 处理以确保它不为 null:

at android.widget.Toast.makeText(Toast.java)
at com.siemens.hc.poc.isf.admin.sysmon.SystemMonitorActivity.setUpDeviceOwner(SystemMonitorActivity.java:649)

Toast.makeText(getApplicationContext(), "Not a device owner", Toast.LENGTH_SHORT).show();

我现在已经调试了 6 个多小时,但我感觉还没有更接近找出问题所在。非常感谢任何帮助,谢谢!

<小时/>

更新:我已经按照 Anton Malyshev 的建议尝试了以下方法

@Test
public void onCreateTest000() throws Exception {
ActivityController controller = Robolectric.buildActivity(SystemMonitorActivitySubclass.class).create().start();
Activity activity = (Activity)controller.get();
}

这有相同的结果;

java.lang.NullPointerException
at android.widget.Toast.__constructor__(Toast.java:114)

最佳答案

似乎您忘记在 .get() 之前调用 .start(),因此您在访问 Toast 内的 context 时遇到 NullPointerException,请检查文档: http://robolectric.org/activity-lifecycle/

所以我建议改变

systemMonitorActivitySubclass = Robolectric.buildActivity(SystemMonitorActivitySubclass.class).create().get();

至:

systemMonitorActivitySubclass = Robolectric.buildActivity(SystemMonitorActivitySubclass.class).create().start().get();

也许最好也保留 ActivityController 引用(如示例代码中所示)。

关于java - Robolectric 未正确处理 getApplicationContext() - NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48385221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com