当前位置: 首页 > 知识库问答 >
问题:

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

谷梁昊空
2023-03-14

我目前正在做一个学校的作业,我正在努力与测试部分。出于某种原因,单元测试单独运行时运行良好,但一起运行时就不行了。我知道这与我在他们之间共享对象有关,而我不应该基于我以前的搜索,但我一生都无法找出需要改变什么来解决这个问题。下面是ApplientService类和ApplientServiceTest类的代码。任何帮助都将非常感谢,因为我已经被困在这个问题上一段时间了,现在知道这可能是其他人会立即看到的东西。

ApptionmentServiceTest类


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类


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


 

共有1个答案

淳于凯
2023-03-14

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

您应该为每个测试单独创建一个新的appointmentservice,作为unit test=test,特别是当您的测试实例有一些状态时。

 类似资料:
  • 我有一堆JUnit测试,它们都单独运行。每一个都是一个真正的独立单元测试--被测试的单个类。不需要上下文。我可以在Eclipse中或通过maven/surefire-plugin单独或一起运行它们。 此后,我添加了一个新的集成测试,它利用了Spring上下文等,并使用了SpringJUnit4ClassRunner。一旦我将这个测试添加到我的套件中,任何测试用例都会在这个类失败后运行。 我不确定这

  • 我一直遇到一个奇怪的问题。我的测试用例有一个失败的测试,。但是,如果我单独运行相同的程序,它将运行得非常完美。我不熟悉JUnit,不知道为什么会发生这种情况。 如果我注释掉最后一个测试(已经注释掉),我的所有测试都成功运行!然而,如果我不评论它,一个测试失败,但那不是这个测试!它是失败!

  • 不要与之前提出的问题混淆“为什么我的测试在一起运行时失败,但单独通过?” 我有一个任务,我需要修改JUnit测试类来处理多个数据库测试。在实现之前,我需要确保所有测试都在运行,没有失败。令我困惑的是,现在当我一起运行所有的类时,它显示它运行时没有失败。当我运行一个特定的类时,它突然失败了,如果我重复它,结果仍然存在。 这可能是什么原因造成的? 我自己没有写测试,因此我对测试内容的了解是有限的。不过

  • 这是我的整个测试课程: 有3个单元测试,它们在单独运行时都通过了,但当我运行整个测试类时,我的第2个和第3个测试失败,错误如下: 我已经想尽一切办法来解决这个问题: 我将测试实例化下的类移动到@Before函数中 我尝试创建@After函数并调用Mockito。重置我的模拟 我应该提到的是,我正在使用nhaarman。mockitokotlin2库和argumentCaptor。 关于为什么这些测

  • 我为咖啡因CacheLoader实现编写的单元测试(JUnit,Mockito)在单独运行时都成功了,但在一起运行时其中一个失败了。我相信我在所有测试对象设置中使用了的最佳实践。 当与其他人一起运行时,测试testGet_WhenCalledASecondAndThirdTimeBeyondCacheDuration_LoadingMethodCalledASecondTime每次都会失败,并出现

  • 我有几个JUnit测试,都使用运行。我可以从我的SpringSource工具套件(EclipseJuno)IDE中按类单独运行它们,它们通过了。如果我尝试按模块运行它们(“运行所选项目中的所有测试”),则它们将失败,并出现以下初始化错误: 有什么办法解决吗?甚至故障排除。 吉文斯: JUnit 4.11版