首页 > 解决方案 > 调用另一个类的 JUnit 测试

问题描述

我目前正在尝试创建一个调用另一个类的 JUnit 测试。我已经以我知道的每一种方式工作,但我似乎无法做到正确。GetHistory 测试是引起我所有头痛的测试。任何帮助或提示都会很棒!

package medical.com.medicalApplication.model;
/**
 * 
 * 
 * This class represents a medical record model in the system
 *
 */
public class MedicalRecord {

    private Patient patient;
    private PatientHistory history;


    public MedicalRecord(Patient patient) {
        super();
        this.patient = patient;
        this.history = new PatientHistory();
    }

    public Patient getPatient() {
        return patient;
    }

    public PatientHistory getHistory() {
        return history;
    }   
}

这是我当前的代码:

package medical.com.medicalApplication.model;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import medical.com.medicalApplication.model.PatientHistory;

public class MedicalRecordTest {

    @Test
    public void testGetPatient() {
        String patient = "Perez";
        Patient test = new Patient(patient, patient);
        assertTrue(test.getName().equals(patient));
    }

    @Test
    public void testGetHistory() {
        String history = "Diabetic";
        PatientHistory test = new PatientHistory();
        assertTrue(test.getHistory.equals("Diabetic"));
    }
}

标签: javaeclipsejunit

解决方案


您还没有在这里上所有其他课程。另外请格式化您的代码,以便您的课程正确显示。

如果没有实际文件,我最好的猜测是这些应该返回类的实例。

public Patient getPatient() {
    return this.patient;
}

public PatientHistory getHistory() {
    return this.history;
}

此外,您测试错误。

在这种情况下,您的测试应该使用 assertEquals:

assertEquals(patient.getFirstName(), medicalRecord.getPatient().getFirstName());

如果您使用 assertTrue 您的错误消息将无济于事。这将是:

expected false was true

如果你使用 assertEquals 你的错误信息会更有帮助。这将是

expected "perez" but was "abc"

推荐阅读