【发布时间】:2015-11-02 18:05:54
【问题描述】:
我正在尝试通过使用ActivityUnitTestCase 扩展测试类来为我的应用程序中的活动编写单元测试用例。我可以更早地成功运行测试用例,但现在我在运行它们时总是遇到异常。尽管我对处理NullPointerExceptions 非常熟悉,但我无法弄清楚导致这种情况的问题。我找不到任何类似的问题,所以我发布了这个。
堆栈跟踪显示我的代码中的这一行有一个空对象引用
activity = startActivity(mIntent, null, null);
但是startActivity 方法应该获取我正在测试的活动的实例。我不确定它为什么返回 null。
这是堆栈跟踪。
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'
这里是测试类
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());
}
}
【问题讨论】:
-
@Marcin 这不仅仅是重复。我尝试调试但没有任何线索,也找不到任何相关问题。所以我发了一个。
-
我同意,这不是一个简单的 NullPointerException 案例。该异常是从 Android 的 ActivityUnitTestCase 类的调用链中引发的。
-
@prudnvi 我在编写的类似测试用例中遇到了同样的异常。我在 Lollipop 设备上运行测试。我刚刚在 KitKat 上运行了测试,测试运行成功。您使用的是什么测试运行器?我正在使用 GoogleInstrumentationTestRunner。我想知道我们是否需要升级到新的测试运行器:AndroidJUnitRunner,它位于较新版本的 SDK 中。
-
@Marcin 你能把它取消标记为重复吗?我认为肯定不是。
-
@VitoAndolini 我可以在 Nexus_5_API_22_x86 模拟器 (Lollipop) 中成功运行这些测试用例,无论是否使用 AndroidJUnitRunner。但是测试用例在 Nexus_4_API_19 (Kitkat) 模拟器和三星 S6 设备上都失败了。 (尽管他们都抛出了不同的异常)。 S6 正在抛出 NPE。
标签: android nullpointerexception android-instrumentation activityunittestcase