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
Sandesh Vishwakarma 9Sandesh Vishwakarma 9 

Hello People , Please help me out with the test class for this apex code. Thanks in advance

public class OpportunityFetchField {
    
    @AuraEnabled
    public static list<Opportunity> insertOpportunity(list<Opportunity> oplist, ID ContactId, map<string, string> newObj){
        Contact rec = [SELECT ID ,Name from Contact where ID =: ContactId];
        system.debug('rec '+rec);
        system.debug('oplist '+oplist);
        system.debug('newObj '+newObj);
        for(Opportunity op :oplist){
            op.ContactId = rec.Id;
            op.StageName='Needs Analysis';
        }
        // op.Name= rec.Name;
        //op.CloseDate = Date.today();
        ///***************/
        Integer numberOfRelatedRecords = 0;
        //  Integer SessionNumber = 0;
        List<engageFI_Retail__c> eFIR = [SELECT ID , Name from engageFI_Retail__c where Contact_Name__c =: ContactId];
        System.debug('engageFI_Retail__c---->'+ eFIR);
        System.debug('Size------>'+ eFIR.size());

        
        if(eFIR.size()==5){
            /*----------------------------------------*/
            engageFI_Retail__c eFIRR =  [SELECT id , Name ,Contact_Name__c from engageFI_Retail__c where Contact_Name__c =: ContactId order by createdDate ASC limit 1];
            System.debug('eFIRR--------------'+eFIRR);
            delete eFIRR;
            /*-----------------------------------------*/
            engageFI_Retail__c er = new engageFI_Retail__c();
            
            if(newObj.containskey('BANK_CONVENIENCE__c')){
                er.BANK_CONVENIENCE__c = newObj.get('BANK_CONVENIENCE__c');
            }
            if(newObj.containskey('BANK_COST_EFFECTIVENESS__c')){
                er.BANK_COST_EFFECTIVENESS__c = newObj.get('BANK_COST_EFFECTIVENESS__c');
            }
            if(newObj.containskey('BANK_YIELD__c')){
                er.BANK_YIELD__c = newObj.get('BANK_YIELD__c');
            }
            if(newObj.containskey('BORROW_ACCESS_TO_LIQUIDITY__c')){
                er.BORROW_ACCESS_TO_LIQUIDITY__c = newObj.get('BORROW_ACCESS_TO_LIQUIDITY__c');
            }
            if(newObj.containskey('BORROW_CREDIT_CARDS__c')){
                er.BORROW_CREDIT_CARDS__c = newObj.get('BORROW_CREDIT_CARDS__c');
            }
            if(newObj.containskey('BORROW_DEBT_RESTRUCTURE__c')){
                er.BORROW_DEBT_RESTRUCTURE__c = newObj.get('BORROW_DEBT_RESTRUCTURE__c');
            }
            if(newObj.containskey('PLAN_EMERGENCY_FUND__c')){
                er.PLAN_EMERGENCY_FUND__c = newObj.get('PLAN_EMERGENCY_FUND__c');
            }
            if(newObj.containskey('PLAN_MAJOR_PURCHASES__c')){
                er.PLAN_MAJOR_PURCHASES__c = newObj.get('PLAN_MAJOR_PURCHASES__c');
            }
            if(newObj.containskey('PLAN_RETIREMENT__c')){
                er.PLAN_RETIREMENT__c = newObj.get('PLAN_RETIREMENT__c');
            }
            if(newObj.containskey('PROTECT_ACCOUNT_PROTECTION__c')){
                er.PROTECT_ACCOUNT_PROTECTION__c = newObj.get('PROTECT_ACCOUNT_PROTECTION__c');
            }
            if(newObj.containskey('PROTECT_ESTATE_PLANNING__c')){
                er.PROTECT_ESTATE_PLANNING__c = newObj.get('PROTECT_ESTATE_PLANNING__c');
            }
            if(newObj.containskey('PROTECT_INSURANCE_COVERAGES__c')){
                er.PROTECT_INSURANCE_COVERAGES__c = newObj.get('PROTECT_INSURANCE_COVERAGES__c');
            }  
            er.Scores__c = newObj.get('score');
            er.Contact_Name__c = rec.id;
            String datee = system.now().format('yyyy-MM-dd HH:mm');
            er.Name = rec.Name + ' - ' + datee ;
            insert er;
            
        }
        else if(eFIR.size()<5){
            engageFI_Retail__c er = new engageFI_Retail__c();
            
            if(newObj.containskey('BANK_CONVENIENCE__c')){
                er.BANK_CONVENIENCE__c = newObj.get('BANK_CONVENIENCE__c');
            }
            if(newObj.containskey('BANK_COST_EFFECTIVENESS__c')){
                er.BANK_COST_EFFECTIVENESS__c = newObj.get('BANK_COST_EFFECTIVENESS__c');
            }
            if(newObj.containskey('BANK_YIELD__c')){
                er.BANK_YIELD__c = newObj.get('BANK_YIELD__c');
            }
            if(newObj.containskey('BORROW_ACCESS_TO_LIQUIDITY__c')){
                er.BORROW_ACCESS_TO_LIQUIDITY__c = newObj.get('BORROW_ACCESS_TO_LIQUIDITY__c');
            }
            if(newObj.containskey('BORROW_CREDIT_CARDS__c')){
                er.BORROW_CREDIT_CARDS__c = newObj.get('BORROW_CREDIT_CARDS__c');
            }
            if(newObj.containskey('BORROW_DEBT_RESTRUCTURE__c')){
                er.BORROW_DEBT_RESTRUCTURE__c = newObj.get('BORROW_DEBT_RESTRUCTURE__c');
            }
            if(newObj.containskey('PLAN_EMERGENCY_FUND__c')){
                er.PLAN_EMERGENCY_FUND__c = newObj.get('PLAN_EMERGENCY_FUND__c');
            }
            if(newObj.containskey('PLAN_MAJOR_PURCHASES__c')){
                er.PLAN_MAJOR_PURCHASES__c = newObj.get('PLAN_MAJOR_PURCHASES__c');
            }
            if(newObj.containskey('PLAN_RETIREMENT__c')){
                er.PLAN_RETIREMENT__c = newObj.get('PLAN_RETIREMENT__c');
            }
            if(newObj.containskey('PROTECT_ACCOUNT_PROTECTION__c')){
                er.PROTECT_ACCOUNT_PROTECTION__c = newObj.get('PROTECT_ACCOUNT_PROTECTION__c');
            }
            if(newObj.containskey('PROTECT_ESTATE_PLANNING__c')){
                er.PROTECT_ESTATE_PLANNING__c = newObj.get('PROTECT_ESTATE_PLANNING__c');
            }
            if(newObj.containskey('PROTECT_INSURANCE_COVERAGES__c')){
                er.PROTECT_INSURANCE_COVERAGES__c = newObj.get('PROTECT_INSURANCE_COVERAGES__c');
            }  
            er.Scores__c = newObj.get('score');
            er.Contact_Name__c = rec.id;
            String datee = system.now().format('yyyy-MM-dd HH:mm');
            er.Name = rec.Name + ' - ' + datee ;
            insert er;
        }
        /****************/
        System.debug('ContactId' + ContactId);
        insert oplist;
        system.debug('oplist '+oplist);
        return oplist;
    }
    
    @AuraEnabled
    public static map<Id,engageFI_Retail__c > fetchEngageFIRetailRecentRecord(ID ContactIdd){
        System.debug('ContactIdd------'+ContactIdd);
        map<Id,engageFI_Retail__c > RecordMap = new Map<id,engageFI_Retail__c >();
        
        list<engageFI_Retail__c> eFIRRelatedRecord =  [SELECT id , Name ,BANK_CONVENIENCE__c,BANK_COST_EFFECTIVENESS__c,BANK_YIELD__c,
                                                       BORROW_ACCESS_TO_LIQUIDITY__c ,BORROW_CREDIT_CARDS__c,BORROW_DEBT_RESTRUCTURE__c,
                                                       PLAN_EMERGENCY_FUND__c,PLAN_MAJOR_PURCHASES__c,PLAN_RETIREMENT__c,
                                                       PROTECT_ACCOUNT_PROTECTION__c,PROTECT_ESTATE_PLANNING__c,PROTECT_INSURANCE_COVERAGES__c,
                                                       Scores__c,
                                                       Contact_Name__c,CreatedDate from engageFI_Retail__c where Contact_Name__c =: ContactIdd order by createdDate DESC limit 1];
        for(engageFI_Retail__c  RE : eFIRRelatedRecord ){
            RecordMap.put(Re.Id,Re);
            system.Debug('***'+RecordMap.values());
        }
        
        return RecordMap;
    }
    
}
Best Answer chosen by Sandesh Vishwakarma 9
CharuDuttCharuDutt
Hii Sandesh
@isTest
public class OpportunityFetchFieldtest {
@isTest
    public static void unitTest(){
        list<Opportunity> lstopp = new list<Opportunity>();
        map<string, string> newObj = new map<string, string>();
        Account Acc = new Account();
        Acc.Name = 'test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId = Acc.Id;
        Insert Con;
        engageFI_Retail__c er = new engageFI_Retail__c();
        //Fill All Required Fields
        er.Contact_Name__c = Con.id;
        Insert er; 
        for(Integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'Test Opp'+i;
            opp.AccountId = Acc.Id;
            opp.CloseDate =System.today();
            opp.StageName = 'Closed Won';
            lstopp.Add(opp);
        }
        
        newObj.put('BANK_CONVENIENCE__c', 'BANK_CONVENIENCE__c');
        newObj.put('BANK_COST_EFFECTIVENESS__c', 'BANK_COST_EFFECTIVENESS__c');
        newObj.put('BANK_YIELD__c', 'BANK_YIELD__c');
        newObj.put('BORROW_ACCESS_TO_LIQUIDITY__c', 'BORROW_ACCESS_TO_LIQUIDITY__c');
        
        Test.startTest();
        OpportunityFetchField.fetchEngageFIRetailRecentRecord(Con.Id);
        OpportunityFetchField.insertOpportunity(lstopp,Con.Id,newObj);
        Test.stopTest();
    }
    @isTest
    public static void unitTestv1(){
        list<Opportunity> lstopp = new list<Opportunity>();
        list<engageFI_Retail__c> lster = new list<engageFI_Retail__c>();
        map<string, string> newObj = new map<string, string>();
        Account Acc = new Account();
        Acc.Name = 'test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId = Acc.Id;
        Insert Con;
        
        for(Integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'Test Opp'+i;
            opp.AccountId = Acc.Id;
            opp.CloseDate =System.today();
            opp.StageName = 'Closed Won';
            lstopp.Add(opp);
            
            engageFI_Retail__c er = new engageFI_Retail__c();
        	//Fill All Required Fields
        	er.Contact_Name__c = Con.id;
        	lster.add(element)(er); 
        }
        insert lster;
        
        newObj.put('BANK_CONVENIENCE__c', 'BANK_CONVENIENCE__c');
        newObj.put('BANK_COST_EFFECTIVENESS__c', 'BANK_COST_EFFECTIVENESS__c');
        newObj.put('BANK_YIELD__c', 'BANK_YIELD__c');
        newObj.put('BORROW_ACCESS_TO_LIQUIDITY__c', 'BORROW_ACCESS_TO_LIQUIDITY__c');
        
        Test.startTest();
        OpportunityFetchField.fetchEngageFIRetailRecentRecord(Con.Id);
        OpportunityFetchField.insertOpportunity(lstopp,Con.Id,newObj);
        Test.stopTest();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

CharuDuttCharuDutt
Hii Sandesh
Try Below Test Class
@isTest
public class OpportunityFetchFieldtest {
@isTest
    public static void unitTest(){
        list<Opportunity> lstopp = new list<Opportunity>();
        map<string, string> newObj = new map<string, string>();
        Account Acc = new Account();
        Acc.Name = 'test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId = Acc.Id;
        Insert Con;
        engageFI_Retail__c er = new engageFI_Retail__c();
        //Fill All Required Fields
        er.Contact_Name__c = Con.id;
        Insert er; 
        for(Integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'Test Opp'+i;
            opp.AccountId = Acc.Id;
            opp.CloseDate =System.today();
            opp.StageName = 'Closed Won';
            lstopp.Add(opp);
        }
        
        newObj.put('BANK_CONVENIENCE__c', 'BANK_CONVENIENCE__c');
        newObj.put('BANK_COST_EFFECTIVENESS__c', 'BANK_COST_EFFECTIVENESS__c');
        newObj.put('BANK_YIELD__c', 'BANK_YIELD__c');
        newObj.put('BORROW_ACCESS_TO_LIQUIDITY__c', 'BORROW_ACCESS_TO_LIQUIDITY__c');
        
        Test.startTest();
        OpportunityFetchField.fetchEngageFIRetailRecentRecord(Con.Id);
        OpportunityFetchField.insertOpportunity(lstopp,Con.Id,newObj);
        Test.stopTest();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
Sandesh Vishwakarma 9Sandesh Vishwakarma 9
Thank you Sir. But I am only getting 60% code coverage. Please have a look at this screenshot once. Thanks
User-added image
CharuDuttCharuDutt
Hii Sandesh
@isTest
public class OpportunityFetchFieldtest {
@isTest
    public static void unitTest(){
        list<Opportunity> lstopp = new list<Opportunity>();
        map<string, string> newObj = new map<string, string>();
        Account Acc = new Account();
        Acc.Name = 'test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId = Acc.Id;
        Insert Con;
        engageFI_Retail__c er = new engageFI_Retail__c();
        //Fill All Required Fields
        er.Contact_Name__c = Con.id;
        Insert er; 
        for(Integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'Test Opp'+i;
            opp.AccountId = Acc.Id;
            opp.CloseDate =System.today();
            opp.StageName = 'Closed Won';
            lstopp.Add(opp);
        }
        
        newObj.put('BANK_CONVENIENCE__c', 'BANK_CONVENIENCE__c');
        newObj.put('BANK_COST_EFFECTIVENESS__c', 'BANK_COST_EFFECTIVENESS__c');
        newObj.put('BANK_YIELD__c', 'BANK_YIELD__c');
        newObj.put('BORROW_ACCESS_TO_LIQUIDITY__c', 'BORROW_ACCESS_TO_LIQUIDITY__c');
        
        Test.startTest();
        OpportunityFetchField.fetchEngageFIRetailRecentRecord(Con.Id);
        OpportunityFetchField.insertOpportunity(lstopp,Con.Id,newObj);
        Test.stopTest();
    }
    @isTest
    public static void unitTestv1(){
        list<Opportunity> lstopp = new list<Opportunity>();
        list<engageFI_Retail__c> lster = new list<engageFI_Retail__c>();
        map<string, string> newObj = new map<string, string>();
        Account Acc = new Account();
        Acc.Name = 'test Account';
        Insert Acc;
        Contact Con = new Contact();
        Con.LastName = 'Test Contact';
        Con.AccountId = Acc.Id;
        Insert Con;
        
        for(Integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'Test Opp'+i;
            opp.AccountId = Acc.Id;
            opp.CloseDate =System.today();
            opp.StageName = 'Closed Won';
            lstopp.Add(opp);
            
            engageFI_Retail__c er = new engageFI_Retail__c();
        	//Fill All Required Fields
        	er.Contact_Name__c = Con.id;
        	lster.add(element)(er); 
        }
        insert lster;
        
        newObj.put('BANK_CONVENIENCE__c', 'BANK_CONVENIENCE__c');
        newObj.put('BANK_COST_EFFECTIVENESS__c', 'BANK_COST_EFFECTIVENESS__c');
        newObj.put('BANK_YIELD__c', 'BANK_YIELD__c');
        newObj.put('BORROW_ACCESS_TO_LIQUIDITY__c', 'BORROW_ACCESS_TO_LIQUIDITY__c');
        
        Test.startTest();
        OpportunityFetchField.fetchEngageFIRetailRecentRecord(Con.Id);
        OpportunityFetchField.insertOpportunity(lstopp,Con.Id,newObj);
        Test.stopTest();
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer