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 ボックスにテキストを入力し、正しい応答が返されたことをアラートで確認するにはどうすればよいでしょうか。