java - 调用另一个类的 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"));
}
}
解决方案
您还没有在这里上所有其他课程。另外请格式化您的代码,以便您的课程正确显示。
如果没有实际文件,我最好的猜测是这些应该返回类的实例。
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"
推荐阅读
- c# - SKCanvasView 事件参数仅包含 Pressed 动作类型
- javascript - 以函数式方式重构 JS 代码 - 闭包
- python - 在 Docker 容器中加载微调的 simpletransformer 模型时出错
- html - 在 Visual Studio 中发生未处理的异常,在取消该错误消息后,我网页上的 css 样式与以前不同
- android-studio - arcore中姿态检测模型的关键点作为输入
- scikit-learn - 无法导入“SelfTrainingClassifier”
- node.js - Flutter 在表单数据中发送多个文件时遇到问题
- web - 如何通过查看源代码了解 Web 应用程序框架
- javascript - 为什么 Object 类主要使用静态方法,而 Array 类主要使用实例方法?
- javascript - 如何使用正则表达式从字符串中获取不同类型的值?