有人有为 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 框中填写一些文本,然后通过警报验证是否给出了正确的响应。