You need to sign in to do that
Don't have an account?
Zander Zumbrunnen
Test Class Issues. Please Help!!!
Hello,
I've been struggling with creating a test class for a custom controller with if statements for a Visualforce page. I have no idea what I'm doing wrong and have looked all over these forums and google for an answer with no luck. If someone could give this a shot and let me know what I am doing wrong that would be amazing.
Here is my controller:
Here is my current test class:
Am I missing something here??
I feel like I have inserted a record that should pass but I guess not.
Please help!
Thanks in advance!
I've been struggling with creating a test class for a custom controller with if statements for a Visualforce page. I have no idea what I'm doing wrong and have looked all over these forums and google for an answer with no luck. If someone could give this a shot and let me know what I am doing wrong that would be amazing.
Here is my controller:
public class SuspectListController { public final Account act; public SuspectListController(ApexPages.StandardController stdController) { this.act = (Account)stdController.getRecord(); } public Id AccountId = ApexPages.currentPage().getParameters().get('id'); public List<HealthCloudGA__EhrEncounter__c> EHRRecords = [SELECT Name, CreatedDate, HealthCloudGA__HospitalizeDischargeDiagnosis__c FROM HealthCloudGA__EhrEncounter__c WHERE HealthCloudGA__Account__c = :AccountId AND CreatedDate = LAST_N_DAYS:365 ORDER BY CreatedDate desc]; public List<String> RType = new List<String>(); public List<String> Name = new List<String>(); public List<String> CDate = new List<String>(); public List<String> Code = new List<String>(); public List<String> Description = new List<String>(); public List<String> getType() { for (HealthCloudGA__EhrEncounter__c encounter : EHRRecords) { if (!Description.contains(encounter.HealthCloudGA__HospitalizeDischargeDiagnosis__c) && encounter.HealthCloudGA__HospitalizeDischargeDiagnosis__c != null) { RType.add('EHR Encounter'); Name.add(encounter.Name); Cdate.add(encounter.CreatedDate.format('MM/dd/yyyy')); Code.add(''); Description.add(encounter.HealthCloudGA__HospitalizeDischargeDiagnosis__c); } } return RType; } public List<String> getName() { return Name; } public List<String> getCDate() { return CDate; } public List<String> getCode() { return Code; } public List<String> getDescription() { return Description; }This controller was created to add field values to a list if they are not on the list already and pass that list to an html table on a Visualforce page (Adding to every list in the first function is a bit funky but it saves from looping through each record in each function). My test class currently passes everything except for the if statement and its contents.
Here is my current test class:
@isTest private class SuspectListTestClass { @testSetup static void testSetup() { Account testaccount = new Account(); testaccount.name = 'Zander Test'; testaccount.Primary_Insurance__c = 'Test Insurance Company'; testaccount.Gender__c = 'Male'; testaccount.County__c = 'Maricopa'; testaccount.RecordTypeId = '012360000004FEPAA2'; insert testaccount; } @isTest static void testEHREncounterPass() { Account act = [SELECT Id FROM Account WHERE Name = 'Zander Test' LIMIT 1]; HealthCloudGA__EhrEncounter__c testencounter1 = new HealthCloudGA__EhrEncounter__c(); testencounter1.HealthCloudGA__Account__c = act.Id; testencounter1.HealthCloudGA__HospitalizeDischargeDiagnosis__c = 'Test'; insert testencounter1; PageReference testpagereference = Page.SuspectList; test.setCurrentPage(testpagereference); SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(act)); testcontroller.Description.add(''); String[] record = testcontroller.getType(); System.assertEquals('EHR Encounter',record[0]); } @isTest static void testEHREncounterFail() { Account act = [SELECT Id FROM Account WHERE Name = 'Zander Test' LIMIT 1]; HealthCloudGA__EhrEncounter__c testencounter2 = new HealthCloudGA__EhrEncounter__c(); testencounter2.HealthCloudGA__Account__c = act.Id; testencounter2.HealthCloudGA__HospitalizeDischargeDiagnosis__c = 'Test'; insert testencounter2; PageReference testpagereference = Page.SuspectList; test.setCurrentPage(testpagereference); SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(act)); testcontroller.Description.add('Test'); testcontroller.getType(); } @isTest static void testSuspectListController() { Account act = [SELECT Id FROM Account WHERE Name = 'Zander Test' LIMIT 1]; PageReference testpagereference = Page.SuspectList; test.setCurrentPage(testpagereference); SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(act)); testcontroller.getName(); testcontroller.getCDate(); testcontroller.getCode(); testcontroller.getDescription(); } }From what I understand you need to have one test where it is meant to pass and one where it is meant to fail. I have made 2 test methods but no matter what I do I can not seem to get one to pass. I get an error from the assertEquals() saying that the index is out of range meaning it did not add "EHR Encounter" to the RType list.
Am I missing something here??
I feel like I have inserted a record that should pass but I guess not.
Please help!
Thanks in advance!
Since the SOQL result is relying on AccountId which is getting fetch through the Id pass in the URL In the test class since this value was not getting set, therefore SOQL was not returning any results, the code was not entering the loop so if statement never runs This is the way to set the value in the test class By setting this, the SOQL will return result, hence the code will enter the loop and the if statement will work.
Hope this explains.
All Answers
Please change that method to the below
Just added the below line
If this help please don't forget to mark it best. Thanks
regards
Andrew
Since the SOQL result is relying on AccountId which is getting fetch through the Id pass in the URL In the test class since this value was not getting set, therefore SOQL was not returning any results, the code was not entering the loop so if statement never runs This is the way to set the value in the test class By setting this, the SOQL will return result, hence the code will enter the loop and the if statement will work.
Hope this explains.
Also, thank you for the explanation, I was unaware of that step. Very helpful!
As noted, I feel that explaining why we do things is sometimes more important that purely supplying code.
Regards
Andrew
101% in agreement