我试图用 ActivityUnitTestCase
扩展测试类编写单元测试案例在我的应用程序活动。我可以成功运行测试用例较早,但现在我总是得到异常,同时运行它们。虽然我是pretty熟悉如何使用 NullPointerException异常
,我无法弄清楚,导致此问题。所以我张贴这一个我找不到任何类似的问题。
堆栈跟踪显示我有一个在我的code此行
空对象引用 活动= startActivity(mIntent,NULL,NULL);
但 startActivity
方法应该接受我测试活动的实例。我不知道为什么它返回null。
下面是堆栈跟踪。
显示java.lang.NullPointerException:尝试写入到字段'android.os.IBinder android.app.ActivityThread.mLastIntendedActivityToken对空对象引用在android.app.Activity.performCreate(Activity.java:6372)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)在android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346)在android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)在com.abc.test.MainActivityTest.access $ 100(MainActivityTest.java:16)在com.abc.test.MainActivityTest $ 1.run(MainActivityTest.java:34)在android.app.Instrumentation $ SyncRunnable.run(Instrumentation.java:1891)在android.os.Handler.handleCallback(Handler.java:739)在android.os.Handler.dispatchMessage(Handler.java:95)在android.os.Looper.loop(Looper.java:145)在android.app.ActivityThread.main(ActivityThread.java:6117)在java.lang.reflect.Method.invoke(本机方法)在java.lang.reflect.Method.invoke(Method.java:372)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)试运行失败:仪表运行失败,原因是显示java.lang.NullPointerException
下面是测试类
公共类MainActivityTest扩展ActivityUnitTestCase< MainActivity> { 私人意图mIntent; 私人MainActivity活动; 公共MainActivityTest(){ 超(MainActivity.class); } @覆盖 保护无效设置()抛出异常{ super.setUp(); //创建一个意图发动目标活动,因为它不会自动由Android仪表启动 mIntent =新意图(getInstrumentation()的getContext(),MainActivity.class。); //测试启动活动隔离,在主线程,以避免断言错误。 getInstrumentation()。runOnMainSync(新的Runnable(){ @覆盖 公共无效的run(){ 活性= startActivity(mIntent,NULL,NULL); } }); } @覆盖 保护无效拆解()抛出异常{ super.tearDown(); } / ** *测试此测试夹具的preconditions。 * / @SmallTest 公共无效测试preconditions(){ assertNotNull(MainActivity是空,getActivity()); } @MediumTest 公共无效testSecondActivityWasLaunchedWithIntent(){ //获取意图下一个活动启动 最终意图launchIntent = getStartedActivityIntent(); //验证意图是不为空。 assertNotNull(意图为空,launchIntent); //验证LaunchActivity说完后 assertTrue(isFinishCalled()); }}
解决方案
有一个很好的机会东西在你的应用程序类(即,x扩展应用程序)崩溃早期。如果这是你会看到这个错误的情况下....结帐您的logcat的痕迹。
I am trying to write Unit test cases for Activities in my app by extending the test class with ActivityUnitTestCase
. I could successfully run the test cases earlier but now I'm always getting the exception while running them. Even though I'm pretty familiar with handling NullPointerExceptions
, I couldn't figure out the problem that's causing this. I couldn't find any similar questions so I'm posting this one.
Stack trace shows me there is a null object reference at this line in my code
activity = startActivity(mIntent, null, null);
But the startActivity
method is supposed to get the instance of the activity I'm testing. I'm not sure why it's returning null.
Here is the Stack trace.
java.lang.NullPointerException: Attempt to write to field 'android.os.IBinder android.app.ActivityThread.mLastIntendedActivityToken' on a null object reference
at android.app.Activity.performCreate(Activity.java:6372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158)
at com.abc.test.MainActivityTest.access$100(MainActivityTest.java:16)
at com.abc.test.MainActivityTest$1.run(MainActivityTest.java:34)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1891)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Test running failed: Instrumentation run failed due to 'java.lang.NullPointerException'
Here is the Test class
public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{
private Intent mIntent;
private MainActivity activity;
public MainActivityTest() {
super(MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
//Create an intent to launch target Activity as it is not automatically started by Android Instrumentation
mIntent = new Intent(getInstrumentation().getContext(), MainActivity.class);
//Start the activity under test in isolation, in the main thread to avoid assertion error.
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
activity = startActivity(mIntent, null, null);
}
});
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Tests the preconditions of this test fixture.
*/
@SmallTest
public void testPreconditions() {
assertNotNull("MainActivity is null", getActivity());
}
@MediumTest
public void testSecondActivityWasLaunchedWithIntent() {
// Get the intent for the next started activity
final Intent launchIntent = getStartedActivityIntent();
//Verify the intent was not null.
assertNotNull("Intent was null", launchIntent);
//Verify that LaunchActivity was finished
assertTrue(isFinishCalled());
}
}
解决方案
There's a good chance something in your application class (i.e. X extends Application) is crashing early on. If that's the case you'll see this error.... Checkout your logcat for traces.