How do you test these setter methods using Junit and Eclipse Java ?
The Junit test fails at testAddTreatment(), testAddAllergy(), and testAddMedication().
...................................................................................................................................................................................................................................................................................................................................................................................................................
Junit test file:
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 class file:
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;
}
}