如何使用Junit和Eclipse测试这些设置方法?

时间:2018-10-01 00:02:59

标签: java eclipse junit

如何使用Junit和Eclipse Java测试这些设置方法?

Junit测试在testAddTreatment(),testAddAllergy()和testAddMedication()处失败。

............................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ................................................... ......

Junit测试文件:

package medical.com.medicalApplication.model;

import static org.junit.Assert.*;

import java.util.List;

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

import medical.com.medicalApplication.model.PatientHistory;
import medical.com.medicalApplication.model.Treatment;
import medical.com.medicalApplication.model.Medication;
import medical.com.medicalApplication.model.Allergey;

public class TestPatientHistory {

private PatientHistory history;
private Treatment treatment;
private Medication medication;
private Allergey allergey;

private List<Treatment> treatmentList;
private List<Medication> medicationList;
private List<Allergey> allergyList;

private List<Patient> patientList;


@Before
public void setUp() throws Exception {

    this.medication = new Medication("Peroxide","9/30/2018","11/30/2018","1");
    this.allergey = new Allergey("Peanut");
    this.treatment = new Treatment("9/30/2018","X-ray","fracture");

    PatientHistory.getReference().addMedication(medication);
    PatientHistory.getReference().addAllergy(allergey);
    PatientHistory.getReference().addTreatment(treatment);

    this.treatmentList = PatientHistory.getReference().getAllTreatments();
    this.medicationList = PatientHistory.getReference().getAllMedications();
    this.allergyList = PatientHistory.getReference().getAlergies();
    //Assign class in the setUp method because there is no consistent order in running the tests
    //StudentService.getReference().assignClass("1234", new Class("CS 210", "1221"));
}


@Test
public void testAddTreatment() {
    assertTrue(history.getAllTreatments().equals(treatmentList));
}

@Test
public void testAddAllergy() {
    assertTrue(history.getAlergies().equals(allergyList));
}

@Test
public void testAddMedication() {
    assertTrue(history.getAllMedications().equals(medicationList));
}

}

PatientHistory类文件:

package medical.com.medicalApplication.model;

import java.util.ArrayList;
import java.util.List;

import medical.com.medicalApplication.model.Treatment;
import medical.com.medicalApplication.model.Medication;
import medical.com.medicalApplication.model.Allergey;
    /**
     * 
     * This class represents a patient history model in the system
     *
     */
public class PatientHistory {

private static PatientHistory reference = new PatientHistory();
private List<Treatment> treatments;
private List<Medication> medications;
private List<Allergey> allergy;

public static PatientHistory getReference() {
    return reference;
}

public PatientHistory() {
/*      this.treatments = new ArrayList<Treatment>();
        this.medications = new ArrayList<Medication>();
        this.allergy = new ArrayList<Allergey>();*/
}

public void addTreatment(Treatment treatment) {
    treatments.add(treatment);
}

public void addAllergy(Allergey allegry) {
    allergy.add(allegry);
}

public void addMedication(Medication medication) {
    if(treatments != null){
        medications.add(medication);
    }
}

public List<Allergey> getAlergies() {
    return allergy;
}

public List<Treatment> getAllTreatments() {
    return treatments;
}

public List<Medication> getAllMedications() {
    return medications;
}

}

1 个答案:

答案 0 :(得分:0)

好的,哥们,您在这里遇到了很多错误的事情。首先,您的列表不会被实例化为新列表,因此它们为空。

  

私人名单治疗;   私人清单药物;   私人清单过敏;

如果不执行私有列表处理= new ArrayList之类的操作,就不能向这些列表添加任何内容,否则会出现空指针异常。

第二个问题,在所有班级中发布您的问题,我不得不模拟您的治疗,药物治疗和变态反应(拼写错误)课程。

单元测试中的第三个问题(或更糟糕的设计)使用setup()方法为PatientHistory创建一个私有成员变量,而不是一个静态的getReference(),就像您有多个单元测试一样,它们将处于损坏状态。

最后,因为我是一个好人,所以您看起来很新,所以我重写了它,运行了单元测试,并让此代码可用于这些类,对于您的代码,我得到了Null指针异常,因为您正在尝试将元素添加到空列表中。在下面查看这些类以及单元测试结果。请接受并支持此答案,因为这花了我30分钟。 ...-邓肯·克雷布斯

您的TestPatientHistory.class


package medical.com.medicalApplication.model;

import static org.junit.Assert.assertTrue;

import java.util.ArrayList; import java.util.List;

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

public class TestPatientHistory {

private PatientHistory history; private Treatment treatment; private Medication medication; private Allergey allergey;

private List treatmentList = new ArrayList(); private List medicationList = new ArrayList(); private List allergyList = new ArrayList();

private List patientList = new ArrayList();

@Before public void setUp() throws Exception {

this.medication = new Medication("Peroxide","9/30/2018","11/30/2018","1");
this.allergey = new Allergey("Peanut");
this.treatment = new Treatment("9/30/2018","X-ray","fracture");

PatientHistory.getReference().addMedication(medication);
PatientHistory.getReference().addAllergy(allergey);
PatientHistory.getReference().addTreatment(treatment);

this.treatmentList = PatientHistory.getReference().getAllTreatments();
this.medicationList = PatientHistory.getReference().getAllMedications();
this.allergyList = PatientHistory.getReference().getAlergies();
//Assign class in the setUp method because there is no consistent order in running the tests
//StudentService.getReference().assignClass("1234", new Class("CS 210", "1221"));

}

@Test public void testAddTreatment() { assertTrue(PatientHistory.getReference().getAllTreatments().equals(treatmentList)); }

@Test public void testAddAllergy() { assertTrue(PatientHistory.getReference().getAlergies().equals(allergyList)); }

@Test public void testAddMedication() { assertTrue(PatientHistory.getReference().getAllMedications().equals(medicationList)); }

}

您的PatientHistory类

this.medication = new Medication("Peroxide","9/30/2018","11/30/2018","1");
this.allergey = new Allergey("Peanut");
this.treatment = new Treatment("9/30/2018","X-ray","fracture");

PatientHistory.getReference().addMedication(medication);
PatientHistory.getReference().addAllergy(allergey);
PatientHistory.getReference().addTreatment(treatment);

this.treatmentList = PatientHistory.getReference().getAllTreatments();
this.medicationList = PatientHistory.getReference().getAllMedications();
this.allergyList = PatientHistory.getReference().getAlergies();
//Assign class in the setUp method because there is no consistent order in running the tests
//StudentService.getReference().assignClass("1234", new Class("CS 210", "1221"));


package medical.com.medicalApplication.model;

import java.util.ArrayList; import java.util.List;

public class PatientHistory {

private static PatientHistory reference = new PatientHistory(); private List treatments = new ArrayList(); private List medications = new ArrayList(); private List allergy = new ArrayList();

public static PatientHistory getReference() { return reference; }

public PatientHistory() { /* this.treatments = new ArrayList(); this.medications = new ArrayList(); this.allergy = new ArrayList();*/ }

public void addTreatment(Treatment treatment) { treatments.add(treatment); }

public void addAllergy(Allergey allegry) { allergy.add(allegry); }

public void addMedication(Medication medication) { if(treatments != null){ medications.add(medication); } }

public List getAlergies() { return allergy; }

public List getAllTreatments() { return treatments; }

public List getAllMedications() {

return medications; } }

code>

public class Medication {


public class Patient {

private String desc;

public Patient(String desc) { 
    this.desc = desc;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

}

}

public Patient(String desc) { 
    this.desc = desc;
}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

private String desc;
private String desc2;
private String desc3;
private String desc4;

public Medication(String desc, String desc2, String desc3, String desc4) { 
    this.desc = desc;
    this.desc2 = desc2;
    this.desc3 = desc3;
    this.desc4 = desc4;

}

public String getDesc() {
    return desc;
}

public void setDesc(String desc) {
    this.desc = desc;
}

public String getDesc2() {
    return desc2;
}

public void setDesc2(String desc2) {
    this.desc2 = desc2;
}

public String getDesc3() {
    return desc3;
}

public void setDesc3(String desc3) {
    this.desc3 = desc3;
}

public String getDesc4() {
    return desc4;
}

public void setDesc4(String desc4) {
    this.desc4 = desc4;
}

最近的单元测试结果...长话短说,学习如何实例化列表,下次发布问题时,可以包括所有课程...

enter image description here

相关问题