+ Start a Discussion
NLakshmanNLakshman 

Need test class for code coverage of below apex class, please help?

public with sharing class opptycon
{
public static void updateContracts(Map<Id, Opportunity> opps){

        List<Contract> contractsToUpdate = new List<Contract>();

        List<Contract> contracts = getContractsByOppIds(opps.keyset());

        String manualOrderOutcome = CustomMetadataServices.getContractSettingsText('Manual_Ordering_Processing');

        for(Opportunity opp : opps.values()){

            List<ContractSortWrapper> contractListForOpportunity = ContractSortWrapper.getAllByOpportunityId(contracts, opp.Id);
            contractListForOpportunity.sort();

            if(!contractListForOpportunity.isEmpty()) {

                if(contractListForOpportunity[0].con.Total_ASOP_Submissions__c >= 1 && opp.Current_Process_State__c == 'Order' && (opp.TBC_Initial_Response__c == NULL || opp.TBC_Initial_Response__c == 'ERROR' ) && String.isBlank(opp.Order_Outcome__c)){


                    contractListForOpportunity[0].con.Order_Submission_Outcome__c = CustomMetadataServices.getContractSettingsText('Work_Support_Team');
                    contractListForOpportunity[0].con.Online_Customer_Experience__c = TRUE;
                    contractListForOpportunity[0].con.Order_Outcome__c = CustomMetadataServices.getContractSettingsText('WSS');
                    contractListForOpportunity[0].con.Order_Kickback_Reason__c = CustomMetadataServices.getKickbackReasonValue('System Error');
                    contractListForOpportunity[0].con.Customer_Email_Comments__c = opp.Customer_Email_Comments__c;
                    
                    opp.Order_Outcome__c = CustomMetadataServices.getContractSettingsText('WSS');
                    opp.Online_Customer_Experience__c = TRUE;

                    contractsToUpdate.add(contractListForOpportunity[0].con);
                }
                else{
                    contractListForOpportunity[0].con.Customer_Email_Comments__c = opp.Customer_Email_Comments__c;
                    contractsToUpdate.add(contractListForOpportunity[0].con);                    
                }
                    
            }

        }

        try{

            update contractsToUpdate;
            system.debug('CONTRACTS UPDATED ' + contractsToUpdate);
        }
        catch(DmlException ex){

            System.debug('ERROR WHILE UPDATING CONTRACTS: ' + ex.getMessage());
        }
    }

    public static List<Contract> getContractsByOppIds(Set<Id> oppIds){

        return [SELECT Order_Submission_Outcome__c, Online_Customer_Experience__c, Order_Outcome__c, Opportunity__c, Order_Kickback_Description__c, Send_Order_Form__c, Total_ASOP_Submissions__c, CreatedDate FROM Contract WHERE Opportunity__c IN :oppIds];
    }

}
ankit bansalankit bansal
What problem are you facing while developing the test class for this code? Please elaborate as it seems a pretty straightforward piece of code.
HARSHIL PARIKH AKA:GovindHARSHIL PARIKH AKA:Govind
At least start doing the test classes like you actually entering a record via UI and see the magic of lines getting code coverage. But however, at the end of the day make sure you do the bulk testing for the trigger... (e.g., put records in loop of 200 etc..)
You can start off something like these below...
 
Opportunity opp = New Opportunity();
Account act = New Account();
Contact con = New Contact();

act.Name = 'Acme Inc.';
// Add all the requie fields
insert act;

con.FirstName = 'John';
con.LastName = 'Doe';
con.accountId = act.Id;
// Add all the require fields
insert Con;

Opp.Name = 'Test opp-1';
Opp.accountId = act.id;
Opp.Stage = 'One of your stage name here';
Opp.ColsedDate  = Date.Today();
// Add all the requie fields
insert Opp;