对不起,如果这是重复的,但是每当我尝试搜索此内容时,我都会获得有关"调用其他方法的测试方法"的结果,这不是我试图澄清的。p>在这里学生。我想知道使用同一类的另一种方法测试方法实际上是一种可接受的方法吗?由于某种原因,它给了我"粗略的感觉"。所以我想确保。
例如:
@BeforeClass
public void setUp(){
appointment = new Appointment("CO","Live & Die",
"10/21/1999 18:00", "10/21/2099 00:00");
}
@Test
public void addAppointmentMethodIncrementsTheNumOfSavedAppointments(){
AppointmentBook appointmentBook = new AppointmentBook();
assertEquals(0, appointmentBook.currentNumOfAppointments());
appointmentBook.addAppointment(appointment);
assertEquals(1, appointmentBook.currentNumOfAppointments());
}
@Test
public void addAppointmentMethodSavesTheAppointmentInTheList(){
AppointmentBook appointmentBook = new AppointmentBook();
appointmentBook.addAppointment(appointment);
boolean result = appointmentBook.checkIfAppointmentAlreadyExists(appointment);
assertEquals(true,result);
}
我不是对第一个测试方法"太困扰",但我不确定第二种方法。
- 您会说
addAppointment()
方法在此中经过了很好的测试案件? - 或我实际上测试
checkIfAppointmentAlreadyExists()
方法? - 可以实际测试测试方法,但仍被视为可接受的单位测试?
这是我要测试的代码,用于参考
public class AppointmentBook {
ArrayList<Appointment> allAppointments = null;
public AppointmentBook(){
allAppointments = new ArrayList<Appointment>();
}
public int currentNumOfAppointments() {
return this.allAppointments.size();
}
public void addAppointment(Appointment appointment) {
this.allAppointments.add(appointment);
}
public boolean checkIfAppointmentAlreadyExists(Appointment appointment) {
return this.allAppointments.contains(appointment);
}
}
只要它们属于同一类,就可以在单个测试用例中包含多种方法。因为最小的单元是 class 而不是 method((。这是单位测试用例。
在第二个测试用例中,您正在验证addAppointment
和checkIfAppointmentAlreadyExists
方法。在我看来,它同时涵盖了两种行为。