You need to sign in to do that
Don't have an account?
Can anyone help me with the test class for this controller class
This class is written for a VF page which will search contacts from contact objects when search button is clicked and when contact is selected it will display dispensory items which are in lookup with contacts (API - patient__c).when one or multiple dispensory items are selected by checkboxes and clicked on submit button it will perform some operations.
CONTROLLER CLASS
MY TEST CLASS WITH ONY 36% CODE COVERAGE.
CONTROLLER CLASS
public class SearchController { public List<contact> searchResults {get; set;} public Contact oContact {get; set;} public Dispensary_Item__c oDispensaryItem {get; set;} public List<string> lstProductId {get; set;} public List<string> lstOppId {get; set;} public boolean isList {get; set;} public string sPatientLastName {get; set;} public string sPatientAccountId {get; set;} public SearchController(ApexPages.StandardController controller) { oDispensaryItem = new Dispensary_Item__c(); lstProductId = new List<string>(); SearchResults = new List<contact>(); SearchResults.clear(); lstOppId = new List<string>(); isList = false; sPatientLastName = ''; } public PageReference search() { SearchResults.clear(); sPatientLastName = ''; sPatientAccountId = ''; SearchResults = [SELECT Id, Name, LastName, FirstName,account.id, Accountid, (SELECT Id, Name, IsChecked__c, Opportunity__c, Practice__c, Product__c, Quantity__c, Price__c FROM Dispensary_Items__r) FROM contact WHERE Id = :oDispensaryItem.Patient__c Order By Name LIMIT 1]; if(SearchResults.size() > 0) { List<Dispensary_Item__c> lstTempDispensaryItem = new List<Dispensary_Item__c>(); lstTempDispensaryItem.addAll(SearchResults.get(0).Dispensary_Items__r); sPatientLastName = SearchResults.get(0).LastName; sPatientAccountId = SearchResults.get(0).AccountId; if(lstTempDispensaryItem.size() > 0) { isList = true; for(Dispensary_Item__c oDispItem : lstTempDispensaryItem) { oDispItem.IsChecked__c = false; } } else { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Related Dispensary Item not found.')); } } else { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'No record found.')); SearchResults.clear(); } return null; } public PageReference processSelected() { string sInsertedOpp = ''; List<string> lstDispensaryItemId = new List<string>(); List<Dispensary_Item__c> lstTempDispensaryItem = new List<Dispensary_Item__c>(); for(Contact oContact : SearchResults) { lstTempDispensaryItem.addAll(oContact.Dispensary_Items__r); } List<Dispensary_Item__c> lstDispensaryItem = new List<Dispensary_Item__c>(); for(Dispensary_Item__c oDispensaryItem : lstTempDispensaryItem){ if(oDispensaryItem.IsChecked__c) { lstDispensaryItem.add(oDispensaryItem); lstDispensaryItemId.add(oDispensaryItem.Id); lstProductId.add(oDispensaryItem.Product__c); lstOppId.add(oDispensaryItem.Opportunity__c); } } Set<string> setProduct = new Set<string>(); setProduct.addAll(lstProductId); if(setProduct.size() != lstProductId.size()) { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please select unique Dispensary Product Item.')); lstProductId.clear(); return null; } if(lstDispensaryItem.size() > 0) { String sQuery = 'SELECT Id'; Set<String> lstFields = new Set<String>(); Map<String,Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); Schema.SObjectType sobjType = schemaMap.get('Opportunity'); Schema.DescribeSObjectResult r = sobjType.getDescribe(); Map <String, Schema.SObjectField> fieldMap = r.fields.getMap(); for(Schema.SObjectField sfield : fieldMap.Values()){ schema.describefieldresult dfield = sfield.getDescribe(); if(dfield.isCreateable()) { lstFields.add(String.valueOf(dfield.getname())); sQuery = sQuery + ', ' + String.valueOf(dfield.getname()); } } sQuery = sQuery + ' FROM Opportunity WHERE Id in ('; if(lstOppId.size() > 0) { integer intCount = 1; for(string oString : lstOppId) { if(intCount < lstOppId.size()) { sQuery = sQuery + '\''+oString+'\','; } else { sQuery = sQuery + '\''+oString+'\')'; } intCount = intCount + 1; } } sQuery = sQuery + ' Order By CreatedDate DESC LIMIT 1'; List<SObject> lstOpportunity = Database.query(sQuery); Sobject oOppnew = sobjType.newSobject(); if(lstOpportunity.size() > 0) { SObject oOpportunity = lstOpportunity.get(0); for(String sField : lstFields) { if(sField != 'OwnerId' && sField != 'Amount' && sField != 'Create_Dispensary_Items__c' && sField != 'Dispensary_Items_Created__c' && sField != 'StageName' && sField != 'Priority__c' && sField != 'Name' && sField != 'Source__c' && sField != 'CloseDate' && sField != 'Tracking_Number__c' && sField != 'AccountId') { oOppnew.put(sField, oOpportunity.get(sField)); } else if(sField == 'Create_Dispensary_Items__c') { oOppnew.put('Create_Dispensary_Items__c', false); } else if(sField == 'Priority__c') { oOppnew.put('Priority__c', 'Other'); } else if(sField == 'Name') { oOppnew.put('Name', sPatientLastName); } else if(sField == 'Source__c') { oOppnew.put('Source__c', ''); } else if(sField == 'CloseDate') { oOppnew.put('CloseDate', Date.valueOf(System.now())); } else if(sField == 'StageName') { oOppnew.put('StageName', 'Order Received'); } else if(sField == 'Tracking_Number__c') { oOppnew.put('Tracking_Number__c', ''); } else if(sField == 'AccountId') { oOppnew.put('AccountId', sPatientAccountId); } } oOppnew.put('IsRefillOpportunity__c', true); insert oOppnew; if(oOppnew.id != null) { sInsertedOpp = oOppnew.Id; List<Opportunity> lstOpportunityPriceBook = [SELECT Id, Pricebook2Id FROM Opportunity WHERE Id = :oOppnew.Id AND Pricebook2Id != null LIMIT 1]; if(lstOpportunityPriceBook.size() > 0) { List<PricebookEntry> lstPricebookEntry = [SELECT Id, Name, Pricebook2Id, Product2Id FROM PricebookEntry where IsActive = true AND Product2Id in: lstProductId AND Pricebook2Id =: lstOpportunityPriceBook.get(0).Pricebook2Id]; if(lstPricebookEntry.size() > 0) { Map<string, string> mapPriEntry = new Map<string, string>(); for(PricebookEntry oPricebookEntry : lstPricebookEntry) { mapPriEntry.put(oPricebookEntry.Product2Id, oPricebookEntry.Id); } if(!mapPriEntry.isEmpty()) { List<OpportunityLineItem> lstOppItemnew = new List<OpportunityLineItem>(); for(Dispensary_Item__c oDispensaryItem : lstDispensaryItem) { OpportunityLineItem oOpportunityLineItem = new OpportunityLineItem(); oOpportunityLineItem.OpportunityId = oOppnew.Id; if(mapPriEntry.containsKey(oDispensaryItem.Product__c)) { oOpportunityLineItem.PricebookEntryId = mapPriEntry.get(oDispensaryItem.Product__c); } oOpportunityLineItem.UnitPrice = oDispensaryItem.Price__c; oOpportunityLineItem.Quantity = oDispensaryItem.Quantity__c; lstOppItemnew.add(oOpportunityLineItem); } if(lstOppItemnew.size() > 0) { insert lstOppItemnew; } oOppnew.put('Create_Dispensary_Items__c', true); update oOppnew; } } } } } } if(sInsertedOpp != null && sInsertedOpp != '') { ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Opportunity has been created: '+sInsertedOpp)); } return null; } }NAME OF VF PAGE - Refill_for_Patients
MY TEST CLASS WITH ONY 36% CODE COVERAGE.
@isTest (SeeAllData = true) private class SearchController_Test{ static void setupTestData(){ test.startTest(); String strRecordTypeId = [Select Id From RecordType Where SobjectType = 'account' and Name = 'Practice'].Id; account acc = new Account(Name='Test Account', Type = 'Customer', BillingCity = 'California', BillingPostalCode = '90001', BillingState = 'California', BillingStreet = 'Test', ShippingCity = 'California', ShippingPostalCode = '90001', ShippingState = 'California', ShippingStreet = 'Test', NPI__c = '123456', DEA__c = '1234567'); insert acc; String conRecordTypeId = [Select Id From RecordType Where SobjectType = 'contact' and Name = 'Patient'].Id; Contact contact_Obj = new Contact(AccountId = acc.id, LastName = 'LastName515', RecordTypeId = conRecordTypeId); Insert contact_Obj; Opportunity oOpportunity = new Opportunity(Name = 'test', AccountId = acc.Id, New_or_Existing_Business__c = 'New Business', Patient__c = contact_Obj.Id, Doctor__c = acc.Id, Priority__c = 'Other', Bill_To_del2__c = 'BillToChooseClinic', Ship_To_del1__c = 'ShipToChooseClinic', Shipping_Preference__c = 'Medallian Free 1', StageName = 'Order Received', CloseDate = Date.Today(), Bill_To_del1__c = acc.Id, Ship_To_Clinic__c = acc.Id, Clinic__c = acc.Id, Pharmacy_Printed__c = true); insert oOpportunity; Dispensary_Item__c dispensary_item_Obj = new Dispensary_Item__c(DOB__c = Date.today(), NPI__c = 'NPI__c489', StickIt_Label_Printed__c = false, Opportunity__c = oOpportunity.id, Patient_History_Reviewed__c = false, Patient_Gender__c = 'Male', Pharmacy_Printed__c = false, Non_CPC_Requested__c = false, PT_Refused_Counseling__c = false, IsAllergy__c = false, Prescription_s_Attached__c = false, Awaiting_Information_Send_Notification__c = false, On_Backorder_Send_Notification__c = false, Too_Soon_To_Refill_Send_Notification__c = false, Awaiting_Payment_Send_Notification__c = false, IsChecked__c = true); Insert dispensary_item_Obj; test.stopTest(); } static testMethod void test_search_UseCase1(){ List<Contact> contact_Obj = [SELECT AccountId,LastName from Contact]; System.assertEquals(true,contact_Obj.size()>0); List<Dispensary_Item__c> dispensary_item_Obj = [SELECT DOB__c,NPI__c,StickIt_Label_Printed__c,Opportunity__c,Patient_History_Reviewed__c,Patient_Gender__c,Pharmacy_Printed__c,Non_CPC_Requested__c,PT_Refused_Counseling__c,IsAllergy__c,Prescription_s_Attached__c,Awaiting_Information_Send_Notification__c,On_Backorder_Send_Notification__c,Too_Soon_To_Refill_Send_Notification__c,Awaiting_Payment_Send_Notification__c, Patient__c, IsChecked__c from Dispensary_Item__c]; System.assertEquals(true,dispensary_item_Obj.size()>0); SearchController obj01 = new SearchController(new ApexPages.StandardController(contact_Obj[0])); obj01.searchResults = contact_Obj; obj01.oContact = contact_Obj[0]; obj01.oDispensaryItem = dispensary_item_Obj[0]; obj01.lstProductId = new List<string>(); obj01.search(); } static testMethod void test_processSelected_UseCase1(){ List<Contact> contact_Obj = [SELECT AccountId,LastName from Contact]; System.assertEquals(true,contact_Obj.size()>0); List<Dispensary_Item__c> dispensary_item_Obj = [SELECT DOB__c,NPI__c,StickIt_Label_Printed__c,Opportunity__c,Patient_History_Reviewed__c,Patient_Gender__c,Pharmacy_Printed__c,Non_CPC_Requested__c,PT_Refused_Counseling__c,IsAllergy__c,Prescription_s_Attached__c,Awaiting_Information_Send_Notification__c,On_Backorder_Send_Notification__c,Too_Soon_To_Refill_Send_Notification__c,Awaiting_Payment_Send_Notification__c from Dispensary_Item__c where IsChecked__c = false]; System.assertEquals(true,dispensary_item_Obj.size()>0); SearchController obj01 = new SearchController(new ApexPages.StandardController(contact_Obj[0])); obj01.searchResults = contact_Obj; obj01.oContact = contact_Obj[0]; obj01.oDispensaryItem = dispensary_item_Obj[0]; obj01.lstProductId = new List<string>(); obj01.processSelected(); } }
try this code