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
Aditya Chauhan 10Aditya Chauhan 10 

test class on opportunity

if(mapOldOpp != null && opp.stageName != mapOldOpp.get(opp.Id).stageName && opp.stageName == PROJECT_STAGE_PENDING_APPROVAL && setRecTypeID.contains(opp.RecordTypeID)){
                setOppDOAId.add(opp.Id);                
            }
           
            if(mapOldOpp != null && opp.stageName != mapOldOpp.get(opp.Id).stageName && opp.stageName == PROJECT_STAGE_APPROVED_FOR_PAYMENT && setRecTypeID.contains(opp.RecordTypeID)){
                setOppPaymentId.add(opp.Id);
            }
        }
        
        system.debug ('@@@ setOppDOAId : ' + setOppDOAId);
        system.debug ('@@@ setOppPaymentId : ' + setOppPaymentId);
        
        //
        if(setOppPaymentId != null && !setOppPaymentId.isEmpty()){
            
            Set<String> pmStatusSet = new Set<String>{'Withdrawn', 'Rejected'};
            list<OpportunityLineItem>lstOppLine = [select id,ESA_Total_Approved_Cost_EI__c,Opportunity.Program_EI__c, Opportunity.Program_EI__r.Pgm_Code_EI__c,
                                                          Opportunity.ImplementerAccount_EI__c, Opportunity.ImplementerAccount_EI__r.Vendor_Number_EI__c, 
                                                          Opportunity.ContractorAccount_EI__c, Opportunity.ContractorAccount_EI__r.Vendor_Number_EI__c,
                                                          ESA_Order_Number_EI__c,GL_Account_Code__c,Measure_Code_EI__c,Payment_EI__c,
                                                          Payee_Attention_To_EI__c, Payee_EI__c, Payee_Mailing_Add_EI__c, Payee_Mailing_City_EI__c,
                                                          Payee_Mailing_State__c, Payee_Mailing_Zip_EI__c, Opportunity.RecordType.DeveloperName,
                                                          Opportunity.Name
                                                          from OpportunityLineItem 
                                                          where OpportunityId IN : setOppPaymentId AND Payment_EI__c = null 
                                                          AND Project_Measure_Status_EI__c NOT IN : pmStatusSet  limit 10000];
                                                          
            map<string,decimal>mapImplementorOrderTotalCost = new map<string,decimal>();
            map<string, OpportunityLineItem> mapImplementorOrderLine = new map<string, OpportunityLineItem>();
            string keyVal ='', keyVal1 = '', keyVal2 = '';
            String sysAdminError = '';

            

I have to write a test class,Can anyone help me to cover this part?
Julien SalensonJulien Salenson
Hi aditya,

You can write something like that / need to be completed :
@isTest
public class YourTestClassName {
    static testMethod void testYourCode() {
		// Create test Opportunity for PROJECT_STAGE_PENDING_APPROVAL 
        Opportunity testOpportunityPENDING = new Opportunity(
            Name = 'Test Opportunity',
            StageName = PROJECT_STAGE_PENDING_APPROVAL , 
            RecordTypeId = 'YourRecordTypeId' // Set the appropriate Record Type Id for setRecTypeID.contains(opp.RecordTypeID)
            // Add other required fields
        );
        insert testOpportunityPENDING;
		
        // Create test Opportunity for PROJECT_STAGE_APPROVED_FOR_PAYMENT
        Opportunity testOpportunityAPPROVED = new Opportunity(
            Name = 'Test Opportunity',
            StageName = PROJECT_STAGE_APPROVED_FOR_PAYMENT, 
            RecordTypeId = 'YourRecordTypeId' // Set the appropriate Record Type Id for setRecTypeID.contains(opp.RecordTypeID)
            // Add other required fields
        );
        insert testOpportunityAPPROVED;

        // Create OpportunityLineItem
        OpportunityLineItem testOpportunityLineItem = new OpportunityLineItem(
            OpportunityId = testOpportunityAPPROVED.Id,
            ESA_Total_Approved_Cost_EI__c = 1000.00, // Set your desired values for these fields
            Payment_EI__c = null, // Set Payment_EI__c as needed
            // Add values for other fields as required
        );
        insert testOpportunityLineItem;

        // Call the method you want to test
        Test.startTest();
        YourClass.yourMethod(); // Replace YourClass and yourMethod with actual class and method names
        Test.stopTest();

        // Assert the expected results
        // You may need to query the data to verify the results as needed
        // Example:
        List<OpportunityLineItem> oppLineItems = [SELECT Id FROM OpportunityLineItem WHERE OpportunityId = :testOpportunity.Id];
        System.assertEquals(1, oppLineItems.size(), 'Expected OpportunityLineItem to be created.');

        // Add more assertions as needed
    }
}