我有一個簡單的應用程序,我試圖測試。它有兩個活動和兩個按鈕(每個按鈕轉到另一個活動 - MainActivity和MainActivity2),我正在嘗試對它進行一些簡單的Robotium測試。我收到以下錯誤:測試Android應用程序:套件構建期間出現異常
java.lang.RuntimeException: Exception during suite construction at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238) at java.lang.reflect.Method.invokeNative(Native Method) 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:1840) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.constructNative(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87) at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73) at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262) at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184) at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5142) at android.app.ActivityThread.access$1500(ActivityThread.java:156) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:157) at android.app.ActivityThread.main(ActivityThread.java:5872) at java.lang.reflect.Method.invokeNative(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at android.view.ViewConfiguration.get(ViewConfiguration.java:338) at android.view.View.(View.java:3490) at android.view.View.(View.java:3547) at android.widget.TextView.(TextView.java:674) at android.widget.Button.(Button.java:107) at android.widget.CompoundButton.(CompoundButton.java:68) at android.widget.CheckBox.(CheckBox.java:68) at android.widget.CheckBox.(CheckBox.java:64) at android.widget.CheckBox.(CheckBox.java:60) at com.example.twoactivities.test.RobotiumTest1.(RobotiumTest1.java:19) ... 18 more
我用盡了一切我能找到,並沒有這些解決方案爲我工作。 (在構造函數沒有參數,檢查構建路徑,以確保JAR文件是在正確的地方,確保我的測試應用程序是「可調試」等)
我的代碼是在這裏:
package com.example.twoactivities.test;
import com.example.twoactivities.*;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;
import android.widget.CheckBox;
import android.app.Activity;
public class RobotiumTest1 extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
public RobotiumTest1() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
//setUp() is run before a test case is started.
//This is where the solo object is created.
solo = new Solo(getInstrumentation(), getActivity());
}
// @Override
// public void tearDown() throws Exception {
// //tearDown() is run after a test case has finished.
// //finishOpenedActivities() will finish all the activities that have been opened during the test execution.
// solo.finishOpenedActivities();
// }
public void testClickButton() throws Exception{
solo.assertCurrentActivity("Expected to be on MainActivity activity", "MainActivity.class");
solo.clickOnButton("Go to page 2");
solo.assertCurrentActivity("Not on Activity 2", MainActivity2.class);
solo.clickOnButton("Go to page 1");
solo.assertCurrentActivity("Not on Activity 1", MainActivity.class);
}
}
任何想法我如何才能讓這個測試運行?
感謝, 斯蒂芬妮
編輯:
MainActivity
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
public class MainActivity extends Activity {
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox1);
final TextView text2 = (TextView) findViewById(R.id.checkBoxTextView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity2(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity2.class);
startActivity(intent);
}
public void onCheckBoxClicked(View view){
if (checkBox.isChecked()) {
text2.setText("Checked");
}
else {
((TextView) findViewById(R.id.checkBoxTextView)).setText("Unchecked");
}
}
}
MainActivity2
package com.example.twoactivities;
import com.example.twoactivities.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void activity1(View view){
Intent intent = new Intent(this,com.example.twoactivities.MainActivity.class);
startActivity(intent);
}
}
實際上我並沒有改變任何東西(我不認爲),因爲它最後成功運行。
9月12日修改: 「solo」對象在我嘗試使用它之後正在初始化。
package com.example.twoactivities.test;
import com.example.twoactivities.*;
import com.example.twoactivities.R;
import com.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import android.widget.CheckBox;
import android.widget.TextView;
public class RobotiumTest1 extends ActivityInstrumentationTestCase2<MainActivity> {
public Solo solo;
private TextView checkBoxText;
public CheckBox checkBox1 ;
public RobotiumTest1() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
//setUp() is run before a test case is started.
//This is where the solo object is created.
solo = new Solo(getInstrumentation(), getActivity());
checkBoxText = (TextView) getActivity().findViewById(R.id.checkBoxTextView);
checkBox1 = (CheckBox) getActivity().findViewById(R.id.checkBox1);
}
public void testClickButton() throws Exception{
solo.assertCurrentActivity("Expected to be on MainActivity activity", "MainActivity");
solo.clickOnButton("Go to page 2");
solo.assertCurrentActivity("Not on Activity 2", MainActivity2.class);
solo.clickOnButton("Go to page 1");
solo.assertCurrentActivity("Not on Activity 1", MainActivity.class);
}
編輯...謝謝。 – Stephanie 2014-09-08 18:20:09
嗯...如果我這樣做,那麼我不能在onCheckBoxClicked中使用它們。他們超出了範圍。 – Stephanie 2014-09-11 18:44:43
修復了在onCreate中實例化的主程序 - 主程序現在可以很好地工作。 JUnit測試失敗,出現上述NullPointerException。(我覺得我們在這裏圈子:) :) – Stephanie 2014-09-12 13:49:05