我是 Android 和 JUnit 测试的新手。我正在尝试为 MyFirstApp 示例提出一些测试用例,如下所述:http: //developer.android.com/training/basics/firstapp/index.html
这是我的测试类的样子:
@Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
setActivityInitialTouchMode(false);
mainActivity = (MainActivity)getActivity();
editText = (EditText) mainActivity.findViewById(
com.example.myfirstapp.R.id.edit_message);
button = (Button) mainActivity.findViewById(
com.example.myfirstapp.R.id.button1);
}
public void testPreconditions(){
assertTrue(editText.getHint().toString().equals(
mainActivity.getString(
com.example.myfirstapp.R.string.edit_message)));
}
public void testUI(){
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
editText.performClick();
}
});
getInstrumentation().waitForIdleSync();
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_B);
this.sendKeys(KeyEvent.KEYCODE_C);
this.sendKeys(KeyEvent.KEYCODE_D);
this.sendKeys(KeyEvent.KEYCODE_E);
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
}
testPrecontions 测试成功。然而,这为“button.performClick();”提供了 NPE
有人可以指出我可能做错了什么吗?
谢谢-Angshu