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
Pratiksha Raut 1Pratiksha Raut 1 

I'm writing a test class where I need to pass old and new values of a record to a method in apex class?

Boolean.valueOf(newRecord.get('Custom_Field__c')) != Boolean.valueOf(oldRecord.get('Custom_Field__c'))
ravi soniravi soni
hi Pratiksha,
get referance from following dummy Examples and Made changes according your requirment.
public class dummyClass {
   public static void dummyMethod(list<Opportunity> lstNewOpp, Map<Id,Opportunity> oldOppMap){
       system.debug('lstNewOpp===> ' + lstNewOpp);
       system.debug('oldOppMap===> ' + oldOppMap);
       for(Opportunity opp : lstNewOpp){
       if(opp.StageName != oldOppMap.get(opp.Id).stageName){
           system.debug('stageName===> ' + opp.StageName);
       }    
       }
       
        
    }
}
 
@isTest
public class dummyClassTest {
@isTest
    public static void insertOpp(){
        list<Opportunity> newLstOpp = new list<Opportunity>();
        Map<Id,Opportunity> OldOppMap = new Map<Id,Opportunity>();
        Opportunity opp = new Opportunity();
        opp.Name = 'test';
        Opp.StageName = 'close won';
        Opp.CloseDate = system.today();
        insert opp;
        OldOppMap.put(opp.Id, opp);
        list<Opportunity> lstOpp = [SELECT Id,Name,StageName From Opportunity Where Id =:opp.Id];
        
        lstOpp[0].Name = 'test 1';
        lstOpp[0].StageName = 'close lost';
        update lstOpp;
        newLstOpp = lstOpp;
      
    dummyClass.dummyMethod(newLstOpp, OldOppMap);
    }
}
And If you have any trigger on your object then you can apply following way.
trigger SLInsertOppTeam on Opportunity (after insert, after update) {
     dmmyClass.dummyMethod(trigger.New, trigger.OldMap);
}

let me know if it helps you and marking it as best.
Thank You



    


 
Suraj Tripathi 47Suraj Tripathi 47

Hi pratiksha,

Can you please share your code with the test class so that I could write the test class.

Thank you

Pratiksha Raut 1Pratiksha Raut 1
Hi Suraj,

This is my Class:
public class CPQ_CTriggerJob{

    public static void publishCTriggerJobFinishedEvent(sObject newRecord, sObject oldRecord, String jobType, String fileName){
        if(!Boolean.valueOf(newRecord.get('C_Trigger_Generate_Document__c')) && Boolean.valueOf(newRecord.get('C_Trigger_Generate_Document__c')) != Boolean.valueOf(oldRecord.get('C_Trigger_Generate_Document__c'))){
            publishCJob(String.valueOf(newRecord.get('Id')), jobType, fileName);
        }
    }
    
    private static void publishCJob(String recordId, String jobType, String fileName){
        C_Trigger_Job__e CJob = new C_Trigger_Job__e();
        CJob.Job_Type__c = jobType;
        CJob.Record_Id__c = recordId;
        fileName = fileName+'%';
        List<Attachment> attachments = [SELECT Id FROM Attachment WHERE ParentId = :CJob.Record_Id__c AND Name LIKE :fileName ORDER BY CreatedDate DESC];
        if(!attachments.isEmpty()){
            CJob.Attachment_Id__c = attachments[0].Id;
        }
        
        List<ContentDocumentLink> files = [SELECT Id, ContentDocumentId, ContentDocument.LatestPublishedVersionId From ContentDocumentLink WHERE LinkedEntityId =:CJob.Record_Id__c AND ContentDocument.Title LIKE :fileName ORDER BY ContentDocument.CreatedDate DESC];
        if(!files.isEmpty()){
            CJob.File_Id__c = files[0].ContentDocumentId;
            CJob.ContentVersionId__c = files[0].ContentDocument.LatestPublishedVersionId;
        }
        
        EventBus.publish(new List<C_Trigger_Job__e>{CJob});
    }
    
    @InvocableMethod
    public static void execute(List<Requests> request){
        publishCJob(request[0].recordId, request[0].jobType, request[0].fileName);
    }
    
    public class Requests{
        @InvocableVariable(label='Record Id' required=true)
        public String recordId;
        @InvocableVariable(label='Job Type' required=true)
        public String jobType;
        @InvocableVariable(label='Generated File Name' required=true)
        public String fileName;
    }
}

Test Class:
@isTest
public class CPQ_CTriggerJobTest {
    
    @TestSetup
    static void createTestData(){
        Account acc = CPQ_TestDataUtility.createAccount('Test Account');
        insert acc;
        Opportunity opp = CPQ_TestDataUtility.createOpportunity(acc.Id);
        insert opp;
        blng__LegalEntity__c le = CPQ_TestDataUtility.createLegalEntity('test le', '0601');
        insert le;
        Quote__c quote = CPQ_TestDataUtility.createQuote(acc.Id, opp.Id);
        insert quote;
    }
    
    @isTest static void test1() {
        //Test.enableChangeDataCapture();
        
        Quote__c thisQuote1 = [SELECT ID,C_Trigger_Generate_Document__c FROM Quote__c LIMIT 1];
        // thisQuote1.C_Trigger_Generate_Document__c = false;
        // update thisQuote1;
        
        Attachment attach=new Attachment();       
        attach.Name='Unit Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId=thisQuote1.id;
        insert attach;
        
        C_Trigger_Job__e newsEvent = new C_Trigger_Job__e(
            //Agreement_Id__c='1243535125',
            Attachment_Id__c = attach.Id,
            File_Id__c= attach.Id,
            //ContentVersionId__c='1243535125',
            Job_Type__c='Test',
            Quote_Id__c=thisQuote1.Id
            //Record_Id__c='1243535125'
            
        );
        
       
        //Test.getEventBus().deliver();
        
        // insert newsEvent;
        Test.startTest();
        
        Quote__c newQuote = [SELECT ID,C_Trigger_Generate_Document__c FROM Quote__c LIMIT 1];
        newQuote.C_Trigger_Generate_Document__c = true;
        update newQuote;
        
        // Call method to publish events
        EventBus.publish(newsEvent);
        
        
        
        
        CPQ_CTriggerJob.publishCTriggerJobFinishedEvent(newQuote, thisQuote1, newsEvent.Job_Type__c, attach.Name);
        Test.stopTest();
        
    }
    
}

Thank you in advance!!