Issue
I am new to Android and JUnit testing. I am trying to come up with some test cases for the MyFirstApp example as explained here: http://developer.android.com/training/basics/firstapp/index.html
Here is how my test class looks like:
@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();
}
});
}
The testPrecontions test succeeds. However this gives NPE for "button.performClick();"
Can someone please point out what I might be doing wrong?
Thanks -Angshu
Solution
Ok, I think I figured it out there needed to be added the call to getInstrumentation().waitForIdleSync()
after the 2nd runOnUiThread call as in the following
public void testUI(){
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
editText.performClick();
}
});
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();
}
});
getInstrumentation().waitForIdleSync();
}
Hope this helps anyone else stuck in a similar situation.
Thanks all! -Angshu
Answered By - Angshu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.