2

我是 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

4

2 回答 2

2

好的,我想我发现需要getInstrumentation().waitForIdleSync()在第二次 runOnUiThread 调用之后添加调用,如下所示

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();
}

希望这可以帮助其他陷入类似情况的人。

谢谢大家!——昂舒

于 2013-02-17T09:49:22.230 回答
0

该按钮没有分配 ID,因此返回#findViewByID并且null您不能调用performClick().null-Object

这应该是您的 GUI 声明(从教程中复制),对吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send" />
</LinearLayout>

添加按钮的 ID 属性,例如通过 EditText,这应该修复视图查找中的错误:

<Button android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/button_send" />
于 2013-02-14T15:46:26.150 回答