我有一个Android应用程序,其中此界面包含3个文本字段。我必须执行单元测试,这有点类似于JUnit测试,它在结果中显示红色/绿色条。例如,如果我点击一个文本字段留空的按钮,如何测试是否抛出任何消息或异常?
欢迎任何建议或例子。我想做这么多的工作。我一直在看下面的尝试很长一段时间,&还没弄明白我应该怎么做。我是Android测试新手,请耐心等待。
段:
public void submitNew(View v){
int hour = tp.getCurrentHour();
int minute = tp.getCurrentMinute();
String apptTime = Integer.toString(hour) + Integer.toString(minute);
if (apptTime.length()<4)
apptTime = "0" + apptTime;
//Original statement commented out
//String apptPlace = et.getText().toString();
//Original statement with invalid input (3rd variable - Place)
String apptPlace = "";
int day = dp.getDayOfMonth();
int month = dp.getMonth()+1;
int year = dp.getYear();
String apptDate = Integer.toString(day) + "/" + Integer.toString(month) + "/" + Integer.toString(year);
String appt = apptDate + "," + apptTime + "," + apptPlace;
addUpcomingSet(appt);
Intent intent = new Intent(v.getContext(), DoctorAppointment.class);
startActivity(intent);
//call returnResult()
if (appt != null) {
result = 1; //implies that obj != empty
}
}
//method to be called within submitNew()
public int returnResult() {
return result;
}
答案 0 :(得分:0)
我有点不确定你的意思是什么,但是如果你只想测试按钮点击时EditText
是否为空白,你可以执行以下操作:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String editTextString = editText.getText().toString();
if (editTextString.isEmpty() {
//do what you want to here
}
}
});
同样,我不完全确定你想做什么,但这会检查按下按钮时EditText
字段是否为空。
答案 1 :(得分:0)
Get the String on button click as follow
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String check = editText1.getText().toString();
if (check.equals("") || check.length()==0) {
//Use your code on condition true
}
}
});