Reputation: 145
Sorry ahead if this is a duplicate, but whenever I try to search this I get results about "testing method that calls other methods" which is not what I am trying to clarify.
Student here. I am wondering whether testing a method with another method from the same class is actually an acceptable approach? For some reason, it gives me that "sketchy feeling". So I wanted to make sure.
For example:
@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);
}
I am not 'too bothered' by the first test method, but I am unsure about the second one.
addAppointment()
method is well tested in this
case? checkIfAppointmentAlreadyExists()
method ?Here is the code I am trying to test, for reference
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);
}
}
Upvotes: 0
Views: 706
Reputation: 336
It is totally okay to include multiple methods in a single test case as long as they belong to the same class. Because the smallest unit is Class and not a method(). And it is Unit test case.
In the second test case you are verifying both addAppointment
and checkIfAppointmentAlreadyExists
methods. It is covering two behavior at the same time which is a good practice in my view.
Upvotes: 1