Issue
I am trying to make a JUnit test that checks whether the inputs for an Appointment object are valid or not. I have finished making all the tests, but whenever I run the test, all of the tests pass except for one. For some reason the test testDateInThePast() is failing. Can someone point out the problem? Here is my code:
Appointment.java
package appointment;
import java.util.Date;
public class Appointment {
private String appointmentId;
private Date appointmentDate;
private String description;
public Appointment(String appointmentId, Date appointmentDate, String description) {
if(appointmentId == null || appointmentId.length() > 10) {
//We will want to test for the exception
throw new IllegalArgumentException("Invalid input");
}
if(appointmentDate == null || appointmentDate.before(new Date())) {
throw new IllegalArgumentException("Invalid date");
}
if(description == null || description.length() > 50) {
throw new IllegalArgumentException("Invalid description");
}
this.appointmentId = appointmentId;
this.appointmentDate = appointmentDate;
this.description = description;
}
public String getAppointmentId() {
return appointmentId;
}
public Date getAppointmentDate() {
return appointmentDate;
}
public String getDescription() {
return description;
}
}
AppointmentTest.java
package test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import appointment.Appointment;
class AppointmentTest {
@SuppressWarnings("deprecation")
@Test
void testAppointment() {
Appointment appointment = new Appointment("12121212", new Date(2023, Calendar.MAY, 26), "This appointment involves a couple things");
assertTrue(appointment.getAppointmentId().equals("12121212"));
assertTrue(appointment.getAppointmentDate().equals(new Date(2023, Calendar.MAY, 26)));
assertTrue(appointment.getDescription().equals("This appointment involves a couple things"));
}
@Test
void testAppointmentIdToLong() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("121212124545", new Date(2023, Calendar.MAY, 26), "This appointment involves a couple things");
});
}
@SuppressWarnings("deprecation")
@Test
void testDateInThePast() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("34345612", new Date(2022, Calendar.JUNE, 26), "This appointment involves a couple things");
});
}
@Test
void testAppointmentDescriptionToLong() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("34345612", new Date(2023, Calendar.MAY, 26), "This appointment description is very long and many people would not bother to read the whole thing and just skip over some parts");
});
}
}
Solution
Consider moving to new java.time.LocalDate
import java.time.LocalDate;
public class Appointment {
private String appointmentId;
private LocalDate appointmentDate;
private String description;
public Appointment(String appointmentId, LocalDate appointmentDate, String description) {
if (appointmentId == null || appointmentId.length() > 10) {
//We will want to test for the exception
throw new IllegalArgumentException("Invalid input");
}
if (appointmentDate == null || appointmentDate.isBefore(LocalDate.now())) {
throw new IllegalArgumentException("Invalid date");
}
if (description == null || description.length() > 50) {
throw new IllegalArgumentException("Invalid description");
}
this.appointmentId = appointmentId;
this.appointmentDate = appointmentDate;
this.description = description;
}
public String getAppointmentId() {
return appointmentId;
}
public LocalDate getAppointmentDate() {
return appointmentDate;
}
public String getDescription() {
return description;
}
}
and:
import Appointment;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.util.Calendar;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AppointmentTest {
@SuppressWarnings("deprecation")
@Test
void testAppointment() {
Appointment appointment = new Appointment("12121212", LocalDate.of(2023, Calendar.MAY, 26), "This appointment involves a couple things");
assertTrue(appointment.getAppointmentId().equals("12121212"));
assertTrue(appointment.getAppointmentDate().equals(LocalDate.of(2023, Calendar.MAY, 26)));
assertTrue(appointment.getDescription().equals("This appointment involves a couple things"));
}
@Test
void testAppointmentIdToLong() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("121212124545", LocalDate.of(2023, Calendar.MAY, 26), "This appointment involves a couple things");
});
}
@SuppressWarnings("deprecation")
@Test
void testDateInThePast() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("34345612", LocalDate.of(2022, Calendar.JUNE, 26), "This appointment involves a couple things");
});
}
@Test
void testAppointmentDescriptionToLong() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
new Appointment("34345612", LocalDate.of(2023, Calendar.MAY, 26), "This appointment description is very long and many people would not bother to read the whole thing and just skip over some parts");
});
}
}
Answered By - Demian
Answer Checked By - Mary Flores (JavaFixing Volunteer)