Tôi đang cố gắng viết các trường hợp kiểm tra Đơn vị cho Hoạt động trong ứng dụng của tôi bằng cách mở rộng lớp thử nghiệm với ActivityUnitTestCase
. Tôi có thể chạy thành công các trường hợp thử nghiệm trước đó nhưng bây giờ tôi luôn nhận được ngoại lệ trong khi chạy chúng. Mặc dù tôi khá quen thuộc với việc xử lý NullPointerExceptions
, tôi không thể tìm ra vấn đề gây ra điều này. Tôi không thể tìm thấy bất kỳ câu hỏi tương tự vì vậy tôi gửi bài này.Bắt lỗi Thiết bị đo đạc không thành công do 'java.lang.NullPointerException'
Stack trace cho thấy tôi có một tham chiếu đối tượng null ở dòng này trong mã của tôi
activity = startActivity(mIntent, null, null);
Nhưng phương pháp startActivity
là nghĩa vụ phải được thể hiện của các hoạt động Tôi đang thử nghiệm. Tôi không chắc tại sao nó lại trả về null.
Đây là dấu vết Stack.
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'
Đây là class Test
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 Đây không phải là chỉ là một bản sao. Tôi đã cố gắng để gỡ lỗi nhưng không có manh mối và cũng không thể tìm thấy bất kỳ câu hỏi liên quan. Vì vậy, tôi đã đăng một. – Prudhvi
Tôi đồng ý, đây không phải là trường hợp NullPointerException đơn giản. Ngoại lệ được ném từ bên trong chuỗi cuộc gọi của lớp ActivityUnitTestCase của Android. –
@prudnvi Tôi có cùng ngoại lệ trong một trường hợp thử nghiệm tương tự mà tôi đã viết. Tôi đã chạy thử nghiệm trên thiết bị Lollipop. Tôi vừa chạy thử nghiệm trên KitKat, và thử nghiệm chạy thành công. Á hậu thử nghiệm nào bạn đang sử dụng? Tôi đang sử dụng GoogleInstrumentationTestRunner. Tôi tự hỏi nếu chúng ta cần phải nâng cấp lên các thử nghiệm mới Á hậu: AndroidJUnitRunner đó là trong phiên bản mới hơn của SDK. –