function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Zander ZumbrunnenZander 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:
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!

 
Best Answer chosen by Zander Zumbrunnen
Abdul KhatriAbdul Khatri
Sure @Andrew

Since the SOQL result is relying on AccountId
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];
which is getting fetch through the Id pass in the URL
public Id AccountId = ApexPages.currentPage().getParameters().get('id');
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
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);
            }
        }
This is the way to set the value in the test class
testpagereference.getParameters().put('id', String.valueOf(act.Id));
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

Abdul KhatriAbdul Khatri
Hi Zander

Please change that method to the below

Just added the below line
testpagereference.getParameters().put('id', String.valueOf(act.Id));
@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;
        testpagereference.getParameters().put('id', String.valueOf(act.Id));
        test.setCurrentPage(testpagereference);
        SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(act));
        testcontroller.Description.add('');
        String[] record = testcontroller.getType();
        System.assertEquals('EHR Encounter',record[0]);
    }

If this help please don't forget to mark it best. Thanks

 
Andrew GAndrew G
@abdul - could you explain to Zander why that line is required?  Understanding is always better than just copying code.

regards
Andrew
Abdul KhatriAbdul Khatri
Sure @Andrew

Since the SOQL result is relying on AccountId
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];
which is getting fetch through the Id pass in the URL
public Id AccountId = ApexPages.currentPage().getParameters().get('id');
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
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);
            }
        }
This is the way to set the value in the test class
testpagereference.getParameters().put('id', String.valueOf(act.Id));
By setting this, the SOQL will return result, hence the code will enter the loop and the if statement will work.

Hope this explains.

 
This was selected as the best answer
Zander ZumbrunnenZander Zumbrunnen
Worked perfectly, thank you so much!

Also, thank you for the explanation, I was unaware of that step. Very helpful!
 
Andrew GAndrew G
@abdul - that was a beautiful explanation.  Thank you for taking the time to respond.

As noted, I feel that explaining why we do things is sometimes more important that purely supplying code.  

Regards
Andrew
Abdul KhatriAbdul Khatri
@Andew
101% in agreement