hello i need help formatting the date in order to for my test to read it. it needs to be formatted in Month day, year here is my code
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
public class Appointment{
private final String appointmentId;
private Date appointmentDate;
private String description;
public Appointment( Date appointmentDate, String desc) {
this.appointmentId = String.valueOf(idGenerator.getAndIncrement());
Date today = new Date();
appointmentDate = today;
if (description == null || description.isBlank()) {
this.description = "NULL";
//If name is longer than 50 characters,
} else if(description.length() > 50) {
this.description = desc;
} else {
this.description = desc;
}
}
public String getAppointmentId() {
return appointmentId;
}
public Date getAppointmentDate() {
return appointmentDate;
}
public String getDescription() {
return description;
}
public void setAppointmentId(String appointmentId) {
}
public void setAppointmentDate (Date appointmentDate) {
}
public void setDescription() {
}
}
here is my test code so far
import static org.junit.jupiter.api.Assertions.*;
import java.util.Calendar;
import java.util.Date;
import org.junit.jupiter.api.Test;
class AppointmentTest {
@Test
void testAppointment() {
Appointment appointment = new Appointment( "(this is where i need help)", "To get computer fixed");
assertTrue(appointment.getAppointmentDate().equals("LocalDate"));
assertTrue(appointment.getDescription().equals("To get computer fixed"));
}
}
I dont know how to make the local date appear on new Appointment creation in my test. is there a special function i can use for local date, or do i need to input the month day and year separately. If that is the case how would i do so?
One reason why this is going to be difficult to test is that java.util.Date
actually represents an instant in time, not a combination of month, day and year; although the designers of this class originally tried to make it represent both. If you try to make it represent a particular calendar date using java.util.Date
, you end up having to do quite a lot of work.
There's a much better class you can use - java.time.LocalDate
. This actually represents the combination of year, month and day; and has some neat methods in it for date arithmetic.
Also, your class has a couple of problems
You can pass in the date that you want an appointment to be made for (well, actually an instant in time, not a date), but your constructor overwrites it with the current instant. I don't think you wanted to do that, because it makes it impossible to actually construct an Appointment
with the chosen date.
You're doing some checks on description
- the field in the class, which really should be checks on desc
.
If you fix up your Appointment
class so that it uses LocalDate
instead of Date
, the constructor might look something like this.
public Appointment(LocalDate appointmentDate, String desc) {
this.appointmentId = String.valueOf(idGenerator.getAndIncrement());
this.appointmentDate = appointmentDate;
if (desc == null || desc.isBlank()) {
this.description = "NULL";
} else if(desc.length() > 50) {
this.description = desc.substring(0, 50);
} else {
this.description = desc;
}
}
You should be then able to test the date handling something like this.
@Test
public void testAppointment() {
LocalDate todaysDate = LocalDate.now();
Appointment appointment = new Appointment( todaysDate, "To get computer fixed");
assertEquals(todaysDate, appointment.getAppointmentDate());
assertEquals("To get computer fixed", appointment.getDescription());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.