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 for Custom Controller with "if" statements

Hi,

I'm quite new to Apex and test classes and I'm having some trouble getting decent code coverage (Preferably 100%). I can get everything except a few if statements covered.

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');

    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];

    List<String> RType = new List<String>();
    List<String> Name = new List<String>();
    List<String> CDate = new List<String>();
    List<String> Code = new List<String>();
    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;
    }

Here is my test class:
@isTest
private class SuspectListTestClass {
    static testMethod void testSuspectListController() {
        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;

        HealthCloudGA__EhrEncounter__c testencounter1 = new HealthCloudGA__EhrEncounter__c();
        testencounter1.HealthCloudGA__Account__c = testaccount.Id;
        testencounter1.HealthCloudGA__HospitalizeDischargeDiagnosis__c = 'Test';
        insert testencounter1;
        
        HealthCloudGA__EhrEncounter__c testencounter2 = new HealthCloudGA__EhrEncounter__c();
        testencounter2.HealthCloudGA__Account__c = testaccount.Id;
        testencounter2.HealthCloudGA__HospitalizeDischargeDiagnosis__c = 'Test';
        insert testencounter2;
        
        HealthCloudGA__EhrEncounter__c testencounter3 = new HealthCloudGA__EhrEncounter__c();
        testencounter3.HealthCloudGA__Account__c = testaccount.Id;
        testencounter3.HealthCloudGA__HospitalizeDischargeDiagnosis__c = null;
        insert testencounter3;

        test.startTest();
        PageReference testpagereference = Page.SuspectList;
        test.setCurrentPage(testpagereference);
        SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(testaccount));
        testcontroller.getType();
        test.stopTest();
    }
}

Like I said earlier the only part that is not passing is the if statement and contents. There is more to this controller and test class but if I can solve this issue I can probably fix the rest.

Also, SuspectList is the name of the Visualforce page I'm trying to reference.

Any information on properly setting up test classes would be much appreciated.

Thanks in advance.
ShirishaShirisha (Salesforce Developers) 
Hi Zander,

Greetings!

Can you please check the below documentations for the sample code to cover the if statements:

https://www.sfdc99.com/2013/10/12/if-statements/

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_error_handling.htm

Best Practices on Test classes:https://www.jitendrazaa.com/blog/salesforce/apex/faq-writing-test-class-in-salesforce/

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Zander ZumbrunnenZander Zumbrunnen
Hi Shirisha,

Thank you for the information on test classes, I've made some changes based on the best practices but I still can't seem to create a record that passes my if statement. 

New 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));
        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 = null;
        insert testencounter2;
        PageReference testpagereference = Page.SuspectList;
        test.setCurrentPage(testpagereference);
        SuspectListController testcontroller = new SuspectListController(new ApexPages.StandardController(act));
        testcontroller.getType();
I made 2 test methods (I will probably have to create 4 to check all possibilities eventually) but the one that is supposed to pass still does not.

Controller:
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);
            }   
        }

The first encounter I created has 'Test' in the field I am making sure is not null and the Description list should be empty and therefore not contain 'Test'. From what I understand this should pass yet I get this error "System.ListException: List index out of bounds: 0" in the log meaning it did not add 'EHR Encounter' to the list.

I feel like it has something to do with !list.contains().
Am I missing something here?