其他分享
首页 > 其他分享> > android-Robolectric:构建失败,空指针异常

android-Robolectric:构建失败,空指针异常

作者:互联网

我最近开始为我的android应用程序开发Robolectric单元测试框架.我正在为此使用Android Studio IDE.

我制作了MainActivity.java,其中有按钮R.id.clickButton,我能够在手机/仿真器中运行该应用程序,并且工作正常.

我制作了MainActivityTest.java,在这里我试图看到按钮不是null,如下所示

private MainActivity activity;

@Before
public void setup() {
    activity = Robolectric.buildActivity(MainActivity.class).get();
}

@Test
public void shouldFail() {
    assertTrue(true);
}

@Test
public void shouldNotBeNull() {
    assertThat(activity).isNotNull();

    Button button = (Button) activity.findViewById(R.id.clickButton);
    assertThat(button).isNotNull();

}

当我使用命令行执行测试时,构建失败,原因为Button button =(Button)activity.findViewById(R.id.clickButton);错误跟踪.我找不到为什么.大家看着这个,希望您能尽快答复我.

堆栈跟踪

java.lang.NullPointerException
    at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:132)
    at android.view.ViewGroup.initViewGroup(ViewGroup.java:447)
    at android.view.ViewGroup.__constructor__(ViewGroup.java:417)
    at android.view.ViewGroup.<init>(ViewGroup.java:416)
    at android.widget.FrameLayout.<init>(FrameLayout.java:93)
    at org.robolectric.tester.android.view.RoboWindow$2.<init>(RoboWindow.java:231)
    at org.robolectric.tester.android.view.RoboWindow.createDecorView(RoboWindow.java:231)
    at org.robolectric.tester.android.view.RoboWindow.access$000(RoboWindow.java:30)
    at org.robolectric.tester.android.view.RoboWindow$1.run(RoboWindow.java:222)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.tester.android.view.RoboWindow.getDecorView(RoboWindow.java:220)
    at org.robolectric.tester.android.view.RoboWindow.findViewById(RoboWindow.java:292)
    at org.robolectric.shadows.ShadowActivity.findViewById(ShadowActivity.java:312)
    at android.app.Activity.findViewById(Activity.java)
    at com.example.robolectrichelloworld.MainActivityTest.shouldNotBeNull(MainActivityTest.java:41)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:50)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)

解决方法:

尝试在Robolectric.buildActivity(…)之后插入对create()的调用:

@Before
public void setup()  {
    activity = Robolectric.buildActivity(MainActivity.class).create().get();
}

标签:robolectric,android
来源: https://codeday.me/bug/20191122/2056074.html