triggerssalesforceapextest-class

Test Class in apex


So i am new to salesforce and i finished my training and now im working on a project. But on my project i have stumbled on a test class that i am not finding a way to write it, so i would appreciate if anyone can help me figure out a way to write it. Here is the code:

    public class AP01_Opportunity 
{
    //Method to create a new service contract when opportunity = Gagné
    public static void CreateContract(List<Opportunity> listOpp, Map<Id, Opportunity> oldMap)
    {
        //Variable Declaration
        ServiceContract sc;
        List<ServiceContract> listSCToAdd = new List<ServiceContract>();
        List<ContractLineItem> listContractItems = new List<ContractLineItem>();
        List<Opportunity> listOppGagne = new list<Opportunity>();

        //Loop in list of opportunities
        for(Opportunity opp : listOpp)
        {
            if(opp.StageName == Label.ClotureGagne && !oldMap.get(opp.Id).isWon)
            {
                listOppGagne.add(opp);
            }
        }
        //check if list has opportunity becoming won
        if(listOppGagne.size()  > 0){

            Map<Id, Opportunity> mapOppGagne = new Map<Id, Opportunity> ([SELECT Id,
                                                                          Name,
                                                                          StageName,
                                                                          Pricebook2Id,
                                                                          Account.Name, 
                                                                          (SELECT Id,
                                                                           PricebookEntryId,
                                                                           PricebookEntry.Name, 
                                                                           Quantity, 
                                                                           UnitPrice 
                                                                           FROM OpportunityLineItems)
                                                                          FROM Opportunity
                                                                          WHERE Id in :listOppGagne]);

            for( Opportunity opp : listOppGagne )
            {
                //Create new service contract
                sc = new ServiceContract();
                sc.Name = opp.Name;
                sc.ApprovalStatus = Label.Activated;
                sc.OpportunityId__c = Id.valueOf(opp.Id);
                sc.Pricebook2Id = opp.Pricebook2Id;
                sc.StartDate = Date.today();
                listSCToAdd.add(sc);
            }
            if(listSCToAdd.size() > 0){
                insert listSCToAdd;
                Opportunity currentOpp;
                ContractLineItem cli;
                Id oppId;
                for(ServiceContract servcont : listSCToAdd)
                {
                    oppId = servcont.OpportunityId__c;
                    if(mapOppGagne.containsKey(oppId))
                    {
                        currentOpp = mapOppGagne.get(oppId);
                        //copy the oppLineItems per opportunity to the respective Service Contract
                        for(OpportunityLineItem items : currentOpp.OpportunityLineItems)
                        {
                            cli = new ContractLineItem();
                            cli.PricebookEntryId = items.PricebookEntryId;
                            cli.Quantity = items.Quantity;
                            cli.UnitPrice = items.UnitPrice;
                            cli.ServiceContractId = servcont.Id;
                            listContractItems.add(cli);
                        }
                    }
                }
                if(listContractItems.size() > 0)
                {
                    insert listContractItems; 
                }
            }


        }
    }
}

this code is a trigger that creates a new service contract record with contract line items copied from the opportunity line items, when the opportunity stage changes to "Cloturé Gagné" which means closed won in french.

Thank you in advance.


Solution

  • In order to write a simple test class I would recommend you to use the following guide: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

    The idea is simple: Lets say you create an Opportunity in your Test class and make an insert or update in your case - your trigger class will automatically fire and run the code from your AP01_Opportunity class. You can put some

    System.debug('some message');
    

    to check if your logic works as expected and also which code blocks are executed