2

Android プロジェクトのテストを作成した経験のある人はいますか? テスト プロジェクトのセットアップを開始しましたが、ドキュメントがまったくないことに気付きました。ユーザー名とパスワードのフィールドをテストするテストをセットアップしようとしています。入力のユーザー名またはパスワードを入力せずに機能しています。しかし、今、それらを設定しようとすると、取得し続けjava.lang.NullPointerExceptionますが、理由や方法がわかりません. ここに私が取り組んでいるコードのサンプルがあります。

public class GasTrackerTab1Test extends ActivityInstrumentationTestCase2<GasTrackerTab1> {  
    private Activity mActivity; // MyActivity is the class name of the app under test
    private EditText username;
    private EditText password;
    private Button loginButton;

    @SuppressWarnings("deprecation")
    public GasTrackerTab1Test() {
        super("com.wallproductions.gas.tracker", GasTrackerTab1.class);
    }

    @Override
    protected void setUp() throws Exception {
        /*
         * Call the super constructor (required by JUnit)
         */

        super.setUp();

        /*
         * prepare to send key events to the app under test by turning off touch mode.
         * Must be done before the first call to getActivity()
         */
        setActivityInitialTouchMode(false);

        /*
         * Start the app under test by starting its main activity. The test runner already knows
         * which activity this is from the call to the super constructor, as mentioned
         * previously. The tests can now use instrumentation to directly access the main
         * activity through mActivity.
         */
        mActivity = getActivity();

        username = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_user_name);
        password = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_password);
        loginButton = (Button)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.mainloginbtn);

    } // end of setUp() method definition

    /*
     * Tests the initial values of key objects in the app under test, to ensure the initial
     * conditions make sense. If one of these is not initialized correctly, then subsequent
     * tests are suspect and should be ignored.
     */

    public void testPreconditions() {
        assertNotNull(username);
        assertNotNull(password);
    }

    public void testInvalidUserNamePassword() {
        mActivity.runOnUiThread(
            new Runnable() {
                public void run() {
                    username.setFocus();
                    username.setText("tester");
                    password.setFocus();
                    password.setText("test1234");
                    loginButton.performClick();
                }
            }
        );
    }
}

質問は、これを理解するために見るべき適切なドキュメントはありますか? また、EditText ボックスにテキストを入力し、正しい応答が返されたことをアラートで確認するにはどうすればよいでしょうか。

4

2 に答える 2

0

私は自分の答えを見つけたと信じています

http://www.java2s.com/Open-Source/Android/android-core/platform-tools-tradefederation/com/android/tradefed/uitestapp/EditBoxActivityTest.java.htm

基本的に、メソッドの上に @UiThreadTest を追加し、setText を使用する必要がありました。Runable クラスを使用しない。

public class GasTrackerTab1Test extends ActivityInstrumentationTestCase2<GasTrackerTab1> {  
    private Activity mActivity; // MyActivity is the class name of the app under test
    private EditText username;
    private EditText password;
    private Button loginButton;

    @SuppressWarnings("deprecation")
    public GasTrackerTab1Test() {
        super("com.wallproductions.gas.tracker", GasTrackerTab1.class);
    }

    @Override
    protected void setUp() throws Exception {
        /*
         * Call the super constructor (required by JUnit)
         */

        super.setUp();

        /*
         * prepare to send key events to the app under test by turning off touch mode.
         * Must be done before the first call to getActivity()
         */
        setActivityInitialTouchMode(false);

        /*
         * Start the app under test by starting its main activity. The test runner already knows
         * which activity this is from the call to the super constructor, as mentioned
         * previously. The tests can now use instrumentation to directly access the main
         * activity through mActivity.
         */
        mActivity = getActivity();

        username = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_user_name);
        password = (EditText)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.login_password);
        loginButton = (Button)mActivity.findViewById(com.wallproductions.gas.tracker.R.id.mainloginbtn);

    } // end of setUp() method definition

    /*
     * Tests the initial values of key objects in the app under test, to ensure the initial
     * conditions make sense. If one of these is not initialized correctly, then subsequent
     * tests are suspect and should be ignored.
     */

    public void testPreconditions() {
        assertNotNull(username);
        assertNotNull(password);
    }

    /**
     * Test that the edit box on {@link EditBoxActivity} can focused.
     */
    @UiThreadTest
    public void testUsernameTextFocus() {
        assertNotNull(username);
        assertTrue(username.requestFocus());
        assertTrue(username.hasFocus());
    }

    @UiThreadTest
    public void testPasswordTextFocus() {
        assertNotNull(password);
        assertTrue(password.requestFocus());
        assertTrue(password.hasFocus());
    }

    @UiThreadTest
    public void testInvalidUserNamePassword() {     
        username.requestFocus();
        username.setText("testing");
        password.requestFocus();
        password.setText("whatever");
        loginButton.callOnClick();
    }
}
于 2013-08-28T13:53:00.097 に答える