gpt4 book ai didi

java - Junit 测试单独通过但一起运行时失败

转载 作者:行者123 更新时间:2023-12-05 03:37:08 25 4
gpt4 key购买 nike

我目前正在为学校做作业,但我正在为测试部分而苦苦挣扎。出于某种原因,单元测试在单独运行时运行良好,但在一起运行时运行不佳。我知道这与我在不应该基于我之前的搜索时共享它们之间的对象有关,但我无法终生弄清楚需要更改什么来解决这个问题。下面是 AppointmentService 类和 AppointmentServiceTest 类的代码。任何帮助将不胜感激,因为我已经坚持了一段时间,并且知道其他人可能会立即看到它。

AppointmentServiceTest 类


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

import java.text.ParseException;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import main.Appointment;
import main.AppointmentService;

class AppointmentServiceTest {

private static AppointmentService appointmentService;

@BeforeAll
static void setUp() {
appointmentService = AppointmentService.getService();
}

@Test
void testAddAppointmentSuccess() throws ParseException {
Appointment appointment = new Appointment("123456", "2022-10-01", "Appointment Description String");
assertTrue(appointmentService.addAppointment(appointment));

Appointment cachedAppointment = appointmentService.getAppointment(appointment.getAppointmentId());

assertTrue(cachedAppointment != null);
assertTrue(cachedAppointment.getAppointmentId().equals("123456"));
assertTrue(cachedAppointment.getAppointmentDate().equals("2022-10-01"));
assertTrue(cachedAppointment.getAppointmentDescription().equals("Appointment Description String"));
}

@Test
void testAddMultipleAppointmentsSuccess() throws ParseException {
Appointment appointment1 = new Appointment("123456", "2022-10-01", "Appointment Description String");
Appointment appointment2 = new Appointment("1234567", "2022-10-02", "Appointment Description String2");

assertTrue(appointmentService.addAppointment(appointment1));
Appointment cachedAppointment1 = appointmentService.getAppointment(appointment1.getAppointmentId());

assertTrue(cachedAppointment1 != null);
assertTrue(cachedAppointment1.getAppointmentId().equals("123456"));
assertTrue(cachedAppointment1.getAppointmentDate().equals("2022-10-01"));
assertTrue(cachedAppointment1.getAppointmentDescription().equals("Appointment Description String"));

assertTrue(appointmentService.addAppointment(appointment2));
Appointment cachedAppointment2 = appointmentService.getAppointment(appointment1.getAppointmentId());

assertTrue(cachedAppointment2 != null);
assertTrue(cachedAppointment2.getAppointmentId().equals("1234567"));
assertTrue(cachedAppointment2.getAppointmentDate().equals("2022-10-02"));
assertTrue(cachedAppointment2.getAppointmentDescription().equals("Appointment Description String2"));

}

@Test
void testAddAppoitnmentDuplicateIdFail() throws ParseException {
Appointment appointment1 = new Appointment("123456", "2022-10-01", "Appointment Description String");
Appointment appointment2 = new Appointment("123456", "2022-10-01", "Appointment Description String");


assertTrue(appointmentService.addAppointment(appointment1));
assertFalse(appointmentService.addAppointment(appointment2));
}

@Test
void testGetAppointmentAndUpdateSuccess() throws ParseException {
Appointment appointment = new Appointment("123456", "2022-10-01", "Appointment Description String");
assertTrue(appointmentService.addAppointment(appointment));

Appointment updatedAppointment = appointmentService.getAppointment(appointment.getAppointmentId());
updatedAppointment.setAppointmentDate("2022-10-02");
updatedAppointment.setAppointmentDescription("New Description");

updatedAppointment = appointmentService.getAppointment(updatedAppointment.getAppointmentId());

assertTrue(updatedAppointment.getAppointmentDescription().equals("New Description"));
assertTrue(updatedAppointment.getAppointmentDate().equals("2022-10-02"));
}

@Test
void testGetAppointmentAndDeleteSuccess() throws ParseException {
Appointment appointment = new Appointment("123456", "2022-10-01", "Appointment Description String");

assertTrue(appointmentService.addAppointment(appointment));

appointment = appointmentService.getAppointment(appointment.getAppointmentId());
assertTrue(appointment != null);

assertTrue(appointmentService.deleteAppointment(appointment.getAppointmentId()));
assertTrue(appointmentService.getAppointment(appointment.getAppointmentId()) == null);
}

@Test
void testDeleteInvalidAppointmentFail() {
String invalidAppointmentIdString = "123";

assertFalse(appointmentService.deleteAppointment(invalidAppointmentIdString));
}

和约会服务类


import java.util.HashMap;
import java.util.Map;

public class AppointmentService {

private static AppointmentService reference = new AppointmentService();
private final Map<String, Appointment> appointmentList;

AppointmentService() {
this.appointmentList = new HashMap<String, Appointment>();
}

public static AppointmentService getService() {
return reference;
}

public boolean addAppointment(Appointment appointment) {
boolean isSuccess = false;

if(!appointmentList.containsKey(appointment.getAppointmentId())) {
appointmentList.put(appointment.getAppointmentId(), appointment);
isSuccess = true;
}
return isSuccess;
}

public boolean deleteAppointment(String appointmentId) {
return appointmentList.remove(appointmentId) != null;
}

public Appointment getAppointment(String appointmentId) {
return appointmentList.get(appointmentId);
}

}



最佳答案

您正在为您的 2 测试使用相同的 appointmentService 实例,并在您的 2 测试中添加具有相同 ID(“123456”)的约会。因此,当测试一起运行时,您的下一个测试将始终失败,因为您假设在第二个测试中不存在 ID“123456”,但它确实存在。

您应该为每个测试创建一个新的 appointmentService 作为 unit test = test in isolation,尤其是当您的测试实例具有某些状态时。

关于java - Junit 测试单独通过但一起运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69422083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com