如何进行一个JUnit测试来检查您输入的日期是否无效

tktrz96b  于 2022-11-11  发布在  其他
关注(0)|答案(2)|浏览(136)

我正在尝试进行一个JUnit测试,以检查Appointment对象的输入是否有效。我已经完成了所有的测试,但是每当我运行测试时,除了一个测试之外,所有的测试都通过了。由于某种原因,测试testDateInThePast()失败了。有人能指出这个问题吗?下面是我的代码:
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");
        });
    }

}
z31licg0

z31licg01#

请考虑移至新的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;
    }

}

以及:

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");
        });
    }

}
rwqw0loc

rwqw0loc2#

您面临的是java.util.Date#Date(int, int, int)构造函数的典型陷阱:

Params:
 year – the year minus 1900. 
 month – the month between 0-11. 
 date – the day of the month between 1-31.

请注意年份-年份减去1900,也就是说,您在单元测试中使用的所有日期都是遥远的将来。
附言。
我确实认为检查抛出的异常类型是不够的,因为所有三个分支都抛出相同的IllegalArgumentException-您还需要检查异常消息。

相关问题