我最近开始为我的Android应用程序开发Robolectric单元测试框架。我正在使用Android Studio IDE。
我已经创建了MainActivity.java,其中有按钮R.id.clickButton,我能够在我的手机/模拟器中运行该应用程序并且工作正常。
我已经将MainActivityTest.java设为我想要看到的按钮不是空的,如下所示
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();
}
当我使用命令行执行测试时,按钮按钮=(按钮)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)
答案 0 :(得分:2)
尝试在Robolectric.buildActivity(...)之后插入对create()的调用:
@Before
public void setup() {
activity = Robolectric.buildActivity(MainActivity.class).create().get();
}
答案 1 :(得分:1)
我通过简单地将类扩展为Activity
来解决问题。我的活动正在延长ActionBarActivity
正在创建问题。