通过调用另一个类进行JUnit测试

时间:2020-06-01 01:22:51

标签: java eclipse 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"));
    }
}

1 个答案:

答案 0 :(得分:0)

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

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

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"