Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Junit测试单独通过,但在一起运行时会失败

Junit测试单独通过,但在一起运行时会失败
EN

Stack Overflow用户
提问于 2021-10-02 20:29:00
回答 2查看 92关注 0票数 1

我目前正在做一项学校的作业,我正在努力完成测试部分。由于某些原因,单元测试在单独运行时运行良好,但在一起运行时就不能正常运行。我知道这与我在他们之间共享对象有关,而我不应该基于之前的搜索,但我无论如何也找不出需要改变什么来解决这个问题。下面是AppointmentService类和AppointmentServiceTest类的代码。任何帮助都将非常感谢,因为我已经坚持了一段时间,并且知道这可能是其他人马上就会看到的东西。

AppointmentServiceTest类

代码语言:javascript
运行
AI代码解释
复制
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));
    }

和AppointmentService类

代码语言:javascript
运行
AI代码解释
复制
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);
    }
    
}
EN

回答 2

Stack Overflow用户

发布于 2021-10-02 20:33:43

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

您应该为每个测试创建一个appointmentService = test,特别是当您的测试实例有一些状态时。

票数 3
EN

Stack Overflow用户

发布于 2021-10-02 21:30:45

在此处删除静态

代码语言:javascript
运行
AI代码解释
复制
private static AppointmentService appointmentService;\

对于setUp方法也是如此。另外,将@BeforeAll更改为@BeforeEach,这将使为每个测试创建新的appointmentService以隔离测试用例。

此外,如果没有任何理由让引用成为静态的,那么类本身最好重构(摆脱静态引用),自制的单例是一种反模式。现在,当代码调用getReference时,它总是返回相同的实例,这就是为什么它在运行所有测试时都不能工作。

另请参阅https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html,我希望它能有所帮助

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69422083

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档