繁体   English   中英

JUnit 调用另一个 class 进行测试

[英]JUnit testing with a call to another class

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

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

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

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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM