0

Over here I want to make a jUnit test case by simulating something called an Appointment. But the thing is, I can't use the datatype Time in my test case so to make the object. For example I've heard that it's possible to parse the Time in a String for a test case. Is this possible? If so, is it necessary with what I want to achieve with this test case? Any help would be gladly appreciated!

Here is the code:

Appointment.java

package Medical;

import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Appointment {
    private int appointmentId;
    private String doctorName;
    private String specilizationName;
    private String date;
    private String DateTime;
    private Time time;
    private Time endTime;
    private int appCount;
    private String patientName;
    private String patientId;
    private String doctorId;
    private Boolean Passed;


    public Appointment(){

    }
    public Appointment(int appointmentId, String doctorName, String specilizationName, String date, Time time) throws ParseException {
        super();
        this.appointmentId = appointmentId;
        this.doctorName = doctorName;
        this.specilizationName = specilizationName;
        this.date = date;
        this.time = time;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        this.DateTime = date + sdf.format(time);
        setPassed();
    }
    public int getAppointmentId() {
        return appointmentId;
    }
    public void setAppointmentId(int appointmentId) {
        this.appointmentId = appointmentId;
    }
    public String getDoctorName() {
        return doctorName;
    }
    public void setDoctorName(String doctorName) {
        this.doctorName = doctorName;
    }
    public String getSpecilizationName() {
        return specilizationName;
    }
    public void setSpecilizationName(String specilizationName) {
        this.specilizationName = specilizationName;
    }
    public String getDate() {
        return date;
    }
    public void setDate(String date) {
        this.date = date;
    }
    public Time getTime() {
        return time;
    }
    public void setTime(Time time) {
        this.time = time;
    }
    public String getDateTime() {
        return this.DateTime;
    }
    public void setDateTime(String date, Time time) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        this.DateTime = date + sdf.format(time);
    }
    public Boolean getPassed() {
        return this.Passed;
    }
    public void setPassed() throws ParseException {
        checkTime();
    }

    public Time getEndTime() {
        return endTime;
    }
    public void setEndTime(Time endTime) {
        this.endTime = endTime;
    }
    public int getAppCount() {
        return appCount;
    }
    public void setAppCount(int appCount) {
        this.appCount = appCount;
    }
    public String getPatientName() {
        return patientName;
    }
    public void setPatientName(String patientName) {
        this.patientName = patientName;
    }
    public String getPatientId() {
        return patientId;
    }
    public void setPatientId(String patientId) {
        this.patientId = patientId;
    }
    public String getDoctorId() {
        return doctorId;
    }
    public void setDoctorId(String doctorId) {
        this.doctorId = doctorId;
    }

    public void checkTime() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        Date dateObject = new Date();
        Date currentTimeDate = sdf.parse(sdf.format(dateObject));
        Date appointmentTimeDate = sdf.parse(DateTime);
        this.Passed = appointmentTimeDate.before(currentTimeDate);
    }
}

AppointmentTest.java

package Medical;

import java.time.LocalDate;
import java.time.LocalTime;

import static org.junit.jupiter.api.Assertions.*;

class AppointmentTest {

    Appointment ap1 = new Appointment(1,
            "John",
            "Dermatologist",
            "2020-08-08",
            "")

}
  • 3
    You wrote an incomplete test class and formulated a bad question: you should clarify what you want to validate in your test. For sure you can parse date and time from a string as you did already with SimpleDateFormat,but it is to little to try to help you. – user1708042 Jun 18 '20 at 13:41
  • 2
    Why do you use java.sql.Time in your Appointment class and why can't you just pass that in your test? – Benjamin Maurer Jun 18 '20 at 13:42
  • @BenjaminMaurer Because I'm using a database. I just want to test my methods to be honest. To see whether I can make an Appointment object in the Test class and call the rest of the methods – Programming boi 123 Jun 18 '20 at 13:45
  • OK, depending on how you map your objects to the Database (e.g., JPA/HIbernate) you don't necessarily have to use that type. – Benjamin Maurer Jun 18 '20 at 13:52
  • 2
    There should be no need to use `java.sql.Time` these days (assuming a reasonably recent JDBC driver). Use `java.time.LocalTime`. See [here](https://stackoverflow.com/questions/32437550/whats-the-difference-between-instant-and-localdatetime): _You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes._ – andrewJames Jun 18 '20 at 14:00
  • I recommend you don’t use `SimpleDateFormat` and `java.sql.Time`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalTime` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Since JDBC 4.2 we can directly save `LocalTime` objects to our database and retrieve them back. – Ole V.V. Jun 18 '20 at 15:50

1 Answers1

0

You can use Time's valueOf method to create an Object from a String:

import java.sql.Time;

new Appointment(1,
            "John",
            "Dermatologist",
            "2020-08-08",
            Time.valueOf("12:00:00"));
Benjamin Maurer
  • 3,602
  • 5
  • 28
  • 49