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
si risi ri 

code coverage for custom setting used in apex class

Hi,I have an Apex class in which custom settings are used and I am unable cover the code coverage. I am using another apex class testclass so that my actual apex class code coverage increased to 48% only. I am unable increase the code coverage to 75% Please anyone help me out. Thanks in advance

Apex Class:
public class InstallationHelper {

 private static Case_Installation_Settings__c settings;

    public static void markUnitComplete( List<Installation__c> newInstalls, Map<Id, Installation__c> oldInstalls)
    {
        
        Set<Id> completedInstallUnitIds = new Set<Id>();
        List<Unit__c> updateUnits = new List<Unit__c>();

        if ( settings == null ) settings = Case_Installation_Settings__c.getInstance();


        if ( newInstalls != null && newInstalls.size() > 0 )
        {
            for ( Installation__c inst : newInstalls )
            {
                Installation__c oldInst;
                if (oldInstalls != null && oldInstalls.containsKey(inst.Id))
                {
                    oldInst = oldInstalls.get(inst.Id);
                }

                if ( DisplayUtils.hasChanged(oldInst, inst, 'Status__c') && inst.Mark_Unit_Complete__c )
                {
                    if ( inst.Unit__c != null )
                    {
                        completedInstallUnitIds.add( inst.Unit__c );
                    }
                }
            }
            List<Unit__c> unitList =  [SELECT id, name, status__c FROM Unit__c WHERE Id IN :completedInstallUnitIds];
           
            if ( completedInstallUnitIds != null && completedInstallUnitIds.size() > 0 )
            {
                for ( Unit__c u :unitList)
                {
                    u.Status__c = settings.Unit_Installed_Status_Name__c;
                    updateUnits.add( u );
                }
            }

            if ( updateUnits != null && updateUnits.size() > 0 )
            {
                update updateUnits;
                
            }
            
        }
    }
    
      public static void updateScheduleIds(list<Installation__c> newList, map<id,Installation__c> oldlist)
    {
        set<id> accountids = new set<id>();
        map<id,Installation__c> compInst = new map<id,Installation__c>();
        list<Installation__c> updateInstallations = new list<Installation__c>();
        List<Installation__c> insList = [Select id,Site__c,After_PSA_Schedule_created__c FROM Installation__c WHERE Site__c IN:accountids];
        for(Installation__c inst : newList)
        {
            if(inst.Monitoring_hours_changed__c == false && oldlist.get(inst.id).Monitoring_hours_changed__c== true)
            {
                accountids.add(inst.Site__c);
                compInst.put(inst.Site__c,inst);
            }
            else if(inst.After_PSA_Schedule_created__c == true && oldlist.get(inst.id).After_PSA_Schedule_created__c == false)
            {
                accountids.add(inst.Site__c);
                compInst.put(inst.Site__c,inst);
            }
            if(!accountids.isEmpty())
            {
                for(Installation__c ins : insList)
                {
                    if(compInst.get(ins.Site__c).id != ins.id)
                    {
                        Installation__c temp = compInst.get(ins.Site__c);
                        ins.Monday_ScheduleID__c = temp.Monday_ScheduleID__c;
                        ins.Tuesday_ScheduleID__c = temp.Tuesday_ScheduleID__c;
                        ins.Wednesday_ScheduleID__c = temp.Wednesday_ScheduleID__c;
                        ins.Thursday_ScheduleID__c = temp.Thursday_ScheduleID__c;
                        ins.Friday_ScheduleID__c = temp.Friday_ScheduleID__c;
                        ins.Saturday_ScheduleID__c = temp.Saturday_ScheduleID__c;
                        ins.Sunday_ScheduleID__c = temp.Sunday_ScheduleID__c;
                        updateInstallations.add(ins);
                    }
                    else if(ins.After_PSA_Schedule_created__c == true && compInst.get(ins.Site__c).id == ins.id)
                    {
                        ins.After_PSA_Schedule_created__c = false;
                        updateInstallations.add(ins);
                    }
                }
            }
        }
        
        if(!updateInstallations.isEmpty())
            update updateInstallations;
    }

}

Test Class:
@isTest
public with sharing class VFC_MonitoringHours_Ext_Test {
    @TestSetup 
    public static void setupData() {
                  
           
            Account a = new Account();
            a.Name = 'SGI Test One';
            a.Type = 'Customer';
            a.Channel__c = 'Reseller';
            a.Activity_Email__c = 'nopetest@test.com';
            a.RecordType = new RecordType( Name = 'Site' );
            insert a;
            
                    
            
            Contact cnt = new Contact();
            cnt.FirstName = 'Test';
            cnt.LastName = 'Contact';
            cnt.AccountID = a.ID;
            insert cnt;
            
            Opportunity o = new Opportunity();
            o.AccountID = a.ID;
            o.Name = 'SGI Test 1';
            o.StageName = 'Discovery'; // '40-Contracts Signed';
            o.CloseDate = Date.today();
            o.Contract_Expiration_Date__c = Date.today();
            o.RecordTypeId = '012o0000000pmDS';
            o.Billing_Schedule__c = 'Monthly';
            o.Payment_Terms__c = 'NET 15';
            o.PiQ_Contact__c = cnt.ID;
            o.Opp_is_Approved_for_Quote__c = true;
            
            insert o;
            
            Case ca = new Case();
            ca.Type ='Account Change';
            ca.Status ='01-New';
            ca.Subject ='test new';
            ca.Description = 'new Description';
            ca.AccountId= a.id;
            ca.ContactId = cnt.id;  
            insert ca;
            
            Installation__c inst = new Installation__c();
            inst.Name__c ='instal';
            inst.Status__c = '0-New';
            inst.Monitoring_Service__c = 'Live';   
            inst.Site__c = a.Id;
            inst.prior_status__c = 'Suspend';
            inst.prior_monitoring_service__c = 'Client';
            inst.Install_Case__c = ca.id;
            inst.Monitoring_hours_changed__c = true;
            inst.Opportunity__c = o.id;       
            insert inst; 
            inst.Monitoring_hours_changed__c = false;

            o.StageName = 'Installed';
            update o;
         
        }
    }
    }
Navin Selvaraj23Navin Selvaraj23
Hi  si ri,
Custom Settings are "Data" and as such are not part of the test-environment.
Just like every object will have no records by default.
 
What you can do is load the custom setting by using:
insert new CustomSetting__c(Field__c = 'Value');

You are inserting here what value in your original custom setting. So, you will get the code coverage  you need.
Please try and let me know if it works.
If it helps, Dont forget to mention this as best answer.

Regards,
Navin 
JayantJayant
Just like you are setting up Account, Contact, Opportunity, Case and Installation__c in your @testSetup method, please also insert a couple of valid records that your code is expecting in the Case_Installation_Settings__c  custom setting and you will be through. I usually prefer inserting custom settings' records first, followed by standard or custom object records since the triggers on these objects might also be using the custom settings.
If you do not want to insert custom settings' records  explicitly, you can export them to a CSV, upload to Static Resource and use Test.loadData method (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_load_data.htm) to populate the custom settings in your @testSetup method. 

I would also suggest you to create a utility class especially for creation of test data since creating individual object records in every test class is cumbersome. You may have a utility class with methods for creating records of different objects/custom settings and call it from actual test class to reuse. Also, in case you do create a test utiliy class, have an option to specify how many records of an object you actually need to be created so that you may perform bulk testing (for a single record just pass 1 as that counter) and its better if utility methods do not perform DMLs themselves but instead return the list of records to actual calling method which can add/modify attributes/fields if required.

Thanks.
Navin Selvaraj23Navin Selvaraj23
Hi Shubam Kumar,

It will not be a good approach to use seeAllData = true. The best approach will be creating test data in the test class. It is the recommandable approach.

Regards,
Navin
Ajay K DubediAjay K Dubedi
Hi,
You have to insert a new Custom Setting record, in your test context like you would normally do with an SObject.
Whatever_custom_setting__c setting = new Whatever_custom_setting__c();
setting.Name = 'Test Setting';
setting.Value__c = 'Whatever';
insert setting;

and then your function should return the newly created test setting.
You can also follow this link:
http://www.infallibletechie.com/2013/12/getting-null-values-from-custom.html

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
si risi ri
Hi!
Navin Selvaraj23
Hi  si ri,
Custom Settings are "Data" and as such are not part of the test-environment.
Just like every object will have no records by default.
 
What you can do is load the custom setting by using:
1insert new CustomSetting__c(Field__c = 'Value');
You are inserting here what value in your original custom setting. So, you will get the code coverage  you need.


I have tried your logic but it is not working.
si risi ri
Hi  Ajay K Dubedi,
Whatever_custom_setting__c setting = new Whatever_custom_setting__c(); setting.Name = 'Test Setting'; setting.Value__c = 'Whatever'; insert setting;

I also tried your code but my code coverage is not increased