【问题标题】:JUnit testing with a call to another class调用另一个类的 JUnit 测试
【发布时间】:2020-09-19 07:01:08
【问题描述】:

我目前正在尝试创建一个调用另一个类的 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"));
    }
}

【问题讨论】:

  • 欢迎来到 StackOverflow。请不要忘记提及您遇到的确切问题(例如编译错误、断言失败等)。

标签: java eclipse junit


【解决方案1】:

您还没有在此处提供所有其他课程。另外请格式化您的代码,以便正确显示您的课程。

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

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"

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多