Android JUnit测试用例因assertNotNull()而失败,但应用程序工作正常:为什么?

Android JUnit测试用例因assertNotNull()而失败,但应用程序工作正常:为什么?,android,eclipse,junit,Android,Eclipse,Junit,我的应用程序运行良好。但是在我的JUnit测试用例中失败了,它将用于assertNotNull()方法的行显示为error行。但是当我运行应用程序时,它工作得很好 这是故障跟踪: junit.framework.AssertionFailedError: Button not allowed to be null at com.example.androidunittest.test.MainActivityUnitTest.testIntentTriggerViaOnClick(MainAct

我的
应用程序运行良好
。但是在我的
JUnit测试用例中失败了
,它将用于
assertNotNull()方法的行显示为error
行。但是当我运行应用程序时,它工作得很好

这是故障跟踪:

junit.framework.AssertionFailedError: Button not allowed to be null
at com.example.androidunittest.test.MainActivityUnitTest.testIntentTriggerViaOnClick(MainActivityUnitTest.java:39)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
此错误的行是:

        buttonId=com.example.androidunit.R.id.button;
        Button view=(Button) activity.findViewById(buttonId);
        assertNotNull("Button not allowed to be null",view);
我还在学习JUnit,这是我遵循的教程。谁能解释一下那里发生了什么事?如果我的申请中有任何错误,我如何更正


任何帮助都将不胜感激

对android也不太熟悉,但您的按钮似乎为空,这就是断言失败的原因。你是说它不是空的,但它可能是空的。这是我的猜测,但我的应用程序运行良好。那么我应该在哪里更正以避免此失败呢?您没有向我们展示您如何在测试中获得
视图
。@dtmilano我更新了代码段。