-1

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;
}

}

  • 2
    It's unclear what you're asking. On one hand you're asking how to test, and on the other you're citing existing failures. – nitind Oct 01 '18 at 00:22
  • 1
    1. What is the error you're getting? 2. What setter methods do you think are failing? 3. What are the data values that you're comparing in your tests? Print them out and edit your question. – shark1608 Oct 01 '18 at 02:25
  • [It is impossible to answer your question because you do not provide a specification of what your code ought to do](https://stackoverflow.com/a/53757321/545127). – Raedwald Dec 13 '18 at 09:00

1 Answers1

0

Okay So Buddy, you got a lot of wrong things here. First your Lists are not getting instantiated as new Lists so they are null.

private List treatments; private List medications; private List allergy;

You can't add anything to these lists without doing something like private List treatments = new ArrayList otherwise you get a null pointer exception.

Second problem, post your questions with all your classes, I had to mock up your Treatment,Medication and Allergey (misspelled) classes.

Third problem (or more of bad design) in a Unit Test use the setup() method to create a private member variable for PatientHistory instead of a static getReference() as if you have multiple unit tests they will have corrupt state.

Lastly, because I'm a nice guy and you seem new at this I rewrote it, ran a unit test and got this code to work with these classes, with your code I was getting Null pointer exceptions because you were trying to add elements to lists that were null. Take a look at these classes below as well as the unit test result. Please accept and upvote this answer as it took me 30 minutes. ... - Duncan Krebs

Your 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)); }

}

Your PatientHistory class


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; } }


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;
}

}

code>

public class Medication {

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;
}

}


public class Allergey {

private String desc;

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

public String getDesc() {
    return desc;
}

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

}


public class Treatment {

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

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


}

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;
}

}

Lastly the unit test results ... long story short, learn how to instantiate your lists and next time you post questions include all the classes is possible...

enter image description here

Duncan Krebs
  • 3,366
  • 2
  • 33
  • 53