Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having a lot of difficulties designing a test for a singleton. I can't turn it into an array. Any recommendations for improvements to

I am having a lot of difficulties designing a test for a singleton. I can't turn it into an array. Any recommendations for improvements to the test design?

Class Being Tested

package medical.com.medicalApplication.services;

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

import medical.com.medicalApplication.model.MedicalRecord; import medical.com.medicalApplication.model.Patient; import medical.com.medicalApplication.model.PatientHistory; /** * * This class uses a singleton pattern to mock a service instead of using dependency injection * * In addition, it stores data in memory only using Lists * */ //FIXME: Spelling Error. MedicalRescordService -> MedicalRecordService public class MedicalRecordService { //FIXME: Spelling Error. MedicalRescordService -> MedicalRecordService private static MedicalRecordService reference = new MedicalRecordService(); private List patients; private List medicalRecords;

//FIXME: Spelling Error MedicalRescordService -> MedicalRecordService public static MedicalRecordService getReference() { return reference; }

//FIXME: Spelling Error MedicalRescordService -> MedicalRecordService MedicalRecordService() { this.patients = new ArrayList(); this.medicalRecords = new ArrayList(); }

public boolean addPatient(String name, String id) { boolean patientAdded = !patients.stream() .anyMatch(patient -> patient.getId().equals(id)); if (patientAdded) { Patient newPatient = new Patient(name, id); //FIXME: Add newHistory variable PatientHistory newHistory = new PatientHistory(); patients.add(newPatient); //FIXME: Add newHistory variable medicalRecords.add(new MedicalRecord(newPatient, newHistory)); } return patientAdded; } public MedicalRecord getMedicalRecord(String patientId) { return medicalRecords.stream() .filter(medicalRecord -> medicalRecord.getPatient().getId().equals(patientId)).findFirst().get(); }

public Patient getPatient(String patientId) { return patients.stream().filter(person -> person.getId().equals(patientId)) .findFirst().get(); }

public List getAllPatients() { return patients; } public List getPatientsWithAllergies(String allergyName){ for(Patient patient : getAllPatients()){ //FIXME: Spelling Error. getAlergies -> getAllergies //FIXME: Remove .findFirst().get()

//1 of 2 branches missed if(getMedicalRecord(patient.getId()).getHistory().getAllergies().stream().filter(allergy -> allergy.getName().equals(allergyName)) != null){ return Collections.singletonList(patient); } } return Collections.emptyList(); //Code not covered by testing } }

Current Test Design

package medical.com.medicalApplication.services.tests;

import static org.junit.Assert.*;

import java.util.List;

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

import medical.com.medicalApplication.services.MedicalRecordService; import medical.com.medicalApplication.model.Allergy; import medical.com.medicalApplication.model.MedicalRecord; import medical.com.medicalApplication.model.Patient;

public class TestMedicalRecordService {

private MedicalRecordService service; private List patientList; @Before public void before() { service = MedicalRecordService.getReference(); service.addPatient("Barack Obama", "4444"); service.addPatient("Bill Clinton", "4242"); service.addPatient("Mr. Peanut", "0022"); patientList = service.getAllPatients(); } @Test public void testGetReference() { assertNotNull("Medical Record Serivce is NULL", service); } @Test public void testAddPatients() { boolean addPatientSuccess = service.addPatient("Barack Obama", "4444"); assertFalse("service added duplicate patient: 4444", addPatientSuccess); assertNotNull("patient list is null", patientList); assertTrue("unable to locate specified patient id", patientList.stream().anyMatch(patient->patient.getId().equals("4444"))); assertTrue("unable to locate specified patient name", patientList.stream().anyMatch(patient->patient.getName().equals("Barack Obama"))); } @Test public void testGetAllPatients() { boolean addBillSuccess = service.addPatient("Bill Clinton", "4242"); assertFalse("service added duplicate patient: 4242", addBillSuccess); assertNotNull("expectedDoctor List is null", patientList); assertTrue("unable to locate Patient: Bill Clinton", patientList.stream().anyMatch(patient->patient.getName().equals("Bill Clinton"))); assertTrue("unable to locate Patient Id: 4242", patientList.stream().anyMatch(patient->patient.getId().equals("4242"))); } @Test public void testGetMedicalRecord() { //act MedicalRecord medicalRecord = service.getMedicalRecord("4242"); Patient patientList = medicalRecord.getPatient(); //assert assertNotNull("medicalRecordService returned null object", medicalRecord); assertEquals("patient:8797 returned incorrect name", patientList.getName(), "Bill Clinton"); assertEquals("patient:Bill Clinton returned incorrect ID", patientList.getId(), "4242"); } @Test public void testGetPatient() { //act Patient actualPatient = service.getPatient("4242"); //assert assertNotNull("actualPatient returned null object", actualPatient); assertEquals("patient:4242 returned incorrect name", actualPatient.getName(), "Bill Clinton"); assertEquals("patient:Bill Clinton returned incorrect ID", actualPatient.getId(), "4242"); } //FIXME: Missing branches @Test public void testGetPatientsWithAllergies() { service.addPatient("Mr. Peanut", "0022"); MedicalRecord record = service.getMedicalRecord("0022"); Allergy allergy = new Allergy("Peanuts");

record.getHistory().addAllergy(allergy); List peanutsPatientList = service.getPatientsWithAllergies("Peanuts"); assertNotNull("allergy object is null", allergy); assertNotNull("record object is null", record); assertNotNull("peanutsPatientList object is null", peanutsPatientList); } @After public void after() { service = null; patientList = null; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

1. Does your voice project confidence? Authority?

Answered: 1 week ago