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
JohnDuraiJohnDurai 

Test class to cover service class

Hi - can someone help me on writting test class for the below code:

public class IncomeRecordService {
    public void processFinancialAccountRecordsForIncome(List<SObject> changedRecords, List<SObject> changedOldRecords)
    {
        Set<Id> financeAccountId = new Set<Id>();
        if(changedRecords != null){
            For (FinServ__FinancialAccount__c financialAccountRec: (List<FinServ__FinancialAccount__c>) changedRecords){
                financeAccountId.add(financialAccountRec.Id);
            }
            if(financeAccountId.size()>0){
                maintainIncomeRecords(financeAccountId); 
            }
        }
    }
    public void processFinancialAccountRoleRecordsForIncome(List<SObject> changedRecords, List<SObject> changedOldRecords){
        Set<Id> financeAccountIds = new Set<Id>();
        Set<Id> financeAccountRoleIds = new Set<Id>();
        For (FinServ__FinancialAccountRole__c finAccountRoleRec: (List<FinServ__FinancialAccountRole__c>) changedRecords){
            financeAccountRoleIds.add(finAccountRoleRec.Id);
        }
        // Id FinancialAccountRoleRecordTypeId = Schema.SObjectType.FinServ__FinancialAccountRole__c.getRecordTypeInfosByDeveloperName().get(FinancialAccountConstants.FAR_OWNER_ROLE).getRecordTypeId();   
        String RecordTypeName = FinancialAccountConstants.FAR_OWNER_ROLE;
        Id FinancialAccountRoleRecordTypeId = RecordTypeUtility.getRecordTypeId('FinServ__FinancialAccountRole__c', RecordTypeName);
        List<FinServ__FinancialAccountRole__c> farList = [select id,FinServ__Active__c,FinServ__FinancialAccount__c,Name__c,Provider_Client_Id__c,FinServ__StartDate__c from FinServ__FinancialAccountRole__c where Id in :financeAccountRoleIds AND FinServ__Active__c = True And RecordTypeId = :FinancialAccountRoleRecordTypeId];
        for(FinServ__FinancialAccountRole__c farRec : farList){
            financeAccountIds.add(farRec.FinServ__FinancialAccount__c);
        }
        if(financeAccountIds.size()>0){
            maintainIncomeRecords(financeAccountIds); 
        }
        
    }
    public void maintainIncomeRecords(Set<Id> FinancialAccountIdSet){
        List<Income__c> listOfIncomeRec = new List<Income__c>();
        // Id FinancialAccountRoleRecordTypeId = Schema.SObjectType.FinServ__FinancialAccountRole__c.getRecordTypeInfosByDeveloperName().get(FinancialAccountConstants.FAR_Owner_Role).getRecordTypeId();   
        String RecordTypeName = FinancialAccountConstants.FAR_OWNER_ROLE;
        Id FinancialAccountRoleRecordTypeId = RecordTypeUtility.getRecordTypeId(FinancialAccountConstants.FINANCIALACCOUNTROLE, RecordTypeName);
        List<FinServ__FinancialAccount__c> financialAccountLst = [select id,(select id,FinServ__Active__c,FinServ__FinancialAccount__c,Name__c,Provider_Client_Id__c,FinServ__StartDate__c from FinServ__FinancialAccountRoles__r where FinServ__Active__c = True And RecordTypeId = :FinancialAccountRoleRecordTypeId) 
                                                                  from FinServ__FinancialAccount__c where Id in :FinancialAccountIdSet
                                                                  AND Income_Amount__c > 0 AND IsActive__c =: FinancialAccountConstants.FA_STATUS_ACTIVE
                                                                  // AND Primary_Owner_Name__c != null
                                                                 ];
        List<Income__c> existingIncomeRecords = [select Id from Income__c where Financial_Account__c in :FinancialAccountIdSet];
        if(existingIncomeRecords.size()>0){
            delete existingIncomeRecords; // isDeleted = true
            Database.emptyRecycleBin(existingIncomeRecords);
        }
        for(FinServ__FinancialAccount__c financialRecord:financialAccountLst){
            Integer countOfFAR = financialRecord.FinServ__FinancialAccountRoles__r.size();
            if(countOfFAR>0){
                for(FinServ__FinancialAccountRole__c financialAccountRolesRecord : financialRecord.FinServ__FinancialAccountRoles__r){
                    Income__c incomeRec = new Income__c();
                    incomeRec.Primary_Owner__c =financialRecord.FinServ__PrimaryOwner__c;
                    incomeRec.Financial_Account__c = financialRecord.Id;
                    incomeRec.Financial_Account_Role__c = financialAccountRolesRecord.Id;
                    incomeRec.Category__c= FinancialAccountConstants.FA_CATEGORY_INVESTMENT_INCOME;
                    incomeRec.Frequency__c= FinancialAccountConstants.FA_FREQUENCY_ANNUALLY;
                    incomeRec.Amount__c=(financialRecord.Income_Amount__c)/countOfFAR; 
                    if(financialRecord.Tax_Status__c==FinancialAccountConstants.FA_Tax_Status_Gross){
                        incomeRec.Taxable__c = FinancialAccountConstants.INCOME_TAXABLE_YES;
                    }else if(financialRecord.Tax_Status__c== FinancialAccountConstants.FA_TAX_STATUS_FREE|| financialRecord.Tax_Status__c==  FinancialAccountConstants.FA_TAX_STATUS_NET)
                    {
                        incomeRec.Taxable__c = FinancialAccountConstants.INCOME_TAXABLE_NO;
                    }else
                    {
                        incomeRec.Taxable__c ='';  
                    }
                    /* incomeRec.RecordTypeId = Schema.SObjectType.Income__c.getRecordTypeInfosByDeveloperName()
.get(FinancialAccountConstants.INCOME_FA_AUTOMATED).getRecordTypeId(); */
                    incomeRec.RecordTypeId = FinancialAccountRoleRecordTypeId;
                    listOfIncomeRec.add(incomeRec);
                }  
            }  
        }
        upsert listOfIncomeRec;
    }
    
    public void processForclearIncomeRecords(List<SObject> changedRecords, List<SObject> changedOldRecords){
        if(changedOldRecords !=null){
           Set<Id> financeAccountIds = new Set<Id>();
        Set<Id> financeAccountRoleIds = new Set<Id>();
        For (FinServ__FinancialAccountRole__c finAccountRoleRec: (List<FinServ__FinancialAccountRole__c>) changedOldRecords){
            financeAccountRoleIds.add(finAccountRoleRec.Id);
        }
        // Id FinancialAccountRoleRecordTypeId = Schema.SObjectType.FinServ__FinancialAccountRole__c.getRecordTypeInfosByDeveloperName().get(FinancialAccountConstants.FAR_OWNER_ROLE).getRecordTypeId();   
        String RecordTypeName = FinancialAccountConstants.FAR_OWNER_ROLE;
        Id FinancialAccountRoleRecordTypeId = RecordTypeUtility.getRecordTypeId(FinancialAccountConstants.FINANCIALACCOUNTROLE, RecordTypeName);
        List<FinServ__FinancialAccountRole__c> farList = [select id,FinServ__Active__c,FinServ__FinancialAccount__c,Name__c,Provider_Client_Id__c,FinServ__StartDate__c from FinServ__FinancialAccountRole__c where Id in :financeAccountRoleIds AND FinServ__Active__c = True And RecordTypeId = :FinancialAccountRoleRecordTypeId];
        for(FinServ__FinancialAccountRole__c farRec : farList){
            financeAccountIds.add(farRec.FinServ__FinancialAccount__c);
        }
        if(financeAccountIds.size()>0){
            clearIncomeRecords(financeAccountIds); 
        }
        }
    } 
    public void clearIncomeRecords(Set<Id> IdSet)
    {
        //  delete[select id from Income__c where (Financial_Account__c in :IdSet Or Financial_Account_Role__c  in :IdSet) AND recordTypeId =:Schema.SObjectType.Income__c.getRecordTypeInfosByDeveloperName().get(FinancialAccountConstants.INCOME_FA_AUTOMATED).getRecordTypeId()];   
        
        List<Income__C> incomeList = [select id,recordtypeId from Income__c where Financial_Account__c =:IdSet Or Financial_Account_Role__c=:IdSet];
        delete incomeList;
        Database.emptyRecycleBin(incomeList);
    }
}
AnudeepAnudeep (Salesforce Developers) 
You need to basically call the following methods and pass the parameters accordingly

IncomeRecordService.processFinancialAccountRecordsForIncome()
IncomeRecordService.processFinancialAccountRoleRecordsForIncome()
IncomeRecordService.maintainIncomeRecords()
IncomeRecordService.processForclearIncomeRecords()

To understand how to populate a list<sObject>, check the following blog post

https://www.nimbleams.com/blog/sobject-test-data-design-pattern/

Let me know if it helps