I am looking for some help creating some examples of JUnit tests. Using the belo
ID: 3752638 • Letter: I
Question
I am looking for some help creating some examples of JUnit tests. Using the below sections of code I am looking for 5 different JUnit test examples, and just a brief (sentence or 2) description of what is being tested.
package medical.com.medicalApplication.model;
/**
*
* This class represents the Doctor data model in the system
*
*/
public class Doctor {
private String name;
private String id;
public Doctor(String name, String id) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "Doctor Name:"+ name + " ID: "+id;
}
}
package medical.com.medicalApplication.model;
/**
*
* This class represents the employee model in the system
*
*/
public class Employee {
private String name;
private String id;
private String password;
public Employee(String name, String id) {
super();
this.name = name;
this.id = id;
this.password = "Open";
}
public String getName() {
return name;
}
public String getId() {
return id;
}
public String getPassword() {
return password;
}
}
package medical.com.medicalApplication.prompts;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import medical.com.medicalApplication.model.Allergey;
import medical.com.medicalApplication.model.MedicalRecord;
import medical.com.medicalApplication.model.Medication;
import medical.com.medicalApplication.model.Patient;
import medical.com.medicalApplication.model.Treatment;
import medical.com.medicalApplication.services.MedicalRescordService;
/**
*
* This class creates the prompts for the medical application
*
*/
public class MedicalRecordPrompt {
private static List<String> prompt = Arrays.asList("","Medical Record Menu", "1 Add a Treatment", "2 Add a Medication",
"3 Print Patient's Treatments", "4 Print Patient's Medications", "5 Add Allergy", "6 Print Allergies", "0 Main Menu");
public void mainPrompt(Scanner scanner) {
int input = -1;
System.out.println("Enter Patient ID:");
String patientId = scanner.next();
Patient patient = MedicalRescordService.getReference().getPatient(patientId);
if (patient != null) {
while (input != 0) {
System.out.println("Patient: " + patient.getName());
prompt.stream().forEach(System.out::println);
input = scanner.nextInt();
switch (input) {
case 1:
addTreatment(scanner, patient.getId());
break;
case 2:
addMedication(scanner, patient.getId());
break;
case 3:
MedicalRescordService.getReference().getMedicalRecord(patientId).getHistory().getAllTreatments()
.forEach(System.out::println);
break;
case 4:
MedicalRescordService.getReference().getMedicalRecord(patientId).getHistory().getAllMedications()
.forEach(System.out::println);
break;
case 5:
addAllergy(scanner, patient.getId());
break;
case 6:
MedicalRescordService.getReference().getMedicalRecord(patientId).getHistory().getAlergies()
.forEach(System.out::println);
break;
case 0:
break;
default:
break;
}
}
} else {
System.out.println("Patient with that ID could not be found");
}
}
private void addAllergy(Scanner scanner, String patientId) {
int input = -1;
while (input != 0) {
System.out.println("Enter Allergy:");
String allergyName = scanner.next();
Allergey allergy = new Allergey(allergyName);
MedicalRecord medicalRecord = MedicalRescordService.getReference().getMedicalRecord(patientId);
if (medicalRecord != null) {
medicalRecord.getHistory().addAllergy(allergy);
} else {
System.err.println("Error! Medical Record is null");
}
System.out.println(
"Would you like to add another Allergy? 1 for Yes 0 To return to the Medical Record Menu");
input = scanner.nextInt();
}
}
public void addTreatment(Scanner scanner, String patientId) {
int input = -1;
while (input != 0) {
System.out.println("Enter the treatment date:");
String treatmentDate = scanner.next();
System.out.println("Enter diagnose:");
String diagnose = scanner.next();
System.out.println("Enter description:");
String description = scanner.next();
Treatment treatment = new Treatment(treatmentDate, diagnose, description);
MedicalRecord medicalRecord = MedicalRescordService.getReference().getMedicalRecord(patientId);
if (medicalRecord != null) {
medicalRecord.getHistory().addTreatment(treatment);
} else {
System.err.println("Error! Medical Record is null");
}
System.out.println(
"Would you like to add another Treatment? 1 for Yes 0 To return to the Medical Record Menu");
input = scanner.nextInt();
}
}
public List<Patient> findAllPatientsWithAllergy(Scanner scanner){
System.out.println("Enter Allergy:");
String allergy = scanner.next();
return MedicalRescordService.getReference().getPatientsWithAllergies(allergy);
}
public void addMedication(Scanner scanner, String patientId) {
int input = -1;
while (input != 0) {
System.out.println("Enter medication name:");
String name = scanner.next();
System.out.println("Enter startDate:");
String startDate = scanner.next();
System.out.println("Enter endDate:");
String endDate = scanner.next();
System.out.println("Enter dose:");
String dose = scanner.next();
Medication medication = new Medication(name, startDate, endDate, dose);
MedicalRecord medicalRecord = MedicalRescordService.getReference().getMedicalRecord(patientId);
if (medicalRecord != null) {
medicalRecord.getHistory().addMedication(medication);
} else {
System.err.println("Error! Medical Record is null");
}
System.out.println(
"Would you like to add another Medication? 1 for Yes 0 To return to the Medical Record Menu");
input = scanner.nextInt();
}
}
}
Explanation / Answer
Please find an example of JUnit Test Case for the above code:-
1. MedicalRecordPromptTestCase.java
import static org.junit.Assert.assertEquals;
import org.junit.BeforeClass;
import org.junit.Test;
public class MedicalRecordPromptTestCase {
static Doctor doctorObj;
static Employee employeeObj;
@BeforeClass
public static void testSetup() {
// first create the test data that we will use in our Test Cases
doctorObj = new Doctor("Gopal Malaker", "452200");
employeeObj = new Employee("Rahul", "123456");
}
@Test
public void testDoctorDetails() {
// Test the Doctor Info via calling toString()
String expectedString = doctorObj.toString();
String actualString = "Doctor Name:Gopal Malaker ID: 452200";
assertEquals(expectedString, actualString);
}
@Test
public void testEmployeeDetails() {
// Test the Employee Password
String expectedString = employeeObj.getPassword();
String actualString = "Open";
assertEquals(expectedString, actualString);
}
@Test
public void testAddAllergy(){
// First Create an allergy object and add it to Medical Record for that Patient ID
Allergey allergy = new Allergey("Allergy1");
// In Medical Record add the patient ID via patient object
// Next check if the Medical Record for that patient ID can be fetched or not
// Then Add the allergy object so created into the Medical History Record
// Verify that addition is success via check of MedicalRecord.getHistory().getAllergy(allergyObject) is not null
}
@Test
public void testfindAllPatientsWithAllergy(){
// Give a demo allergy name
String allergyName = "Allergy1";
// In a List store the result of this:- MedicalRescordService.getReference().getPatientsWithAllergies(allergy)
// Verify that the list.size() > 0, if record exists
}
}
Please let me know in case of any clarifications required. Thanks!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.