很抱歉,如果这是重复的,但是每当我尝试搜索它时,我都会得到关于“调用其他方法的测试方法”的结果,这不是我要澄清的。
这里的学生。我想知道使用同一类中的另一个方法测试一个方法是否实际上是可以接受的方法?由于某种原因,它给了我那种“粗糙的感觉”。所以我想确定。
例如:
@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);
}
}
答案 0 :(得分:1)
在单个测试用例中包含多个方法是完全可以的,只要它们属于同一类即可。因为最小的单位是 Class ,而不是 method()。这是单元测试用例。
在第二个测试用例中,您正在验证addAppointment
和checkIfAppointmentAlreadyExists
方法。在我看来,它同时涵盖了两种行为。