在顶点测试 Class

Test Class in apex

所以我是销售人员的新手,我完成了培训,现在正在从事一个项目。但是在我的项目中,我偶然发现了一个测试 class,我找不到编写它的方法,所以如果有人能帮助我找到一种编写它的方法,我将不胜感激。这是代码:

    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; 
                }
            }


        }
    }
}

此代码是一个触发器,当机会阶段更改为 "Cloturé Gagné" 时,它会创建一个新的服务合同记录,其中包含从机会行项目复制的合同行项目,这意味着在法语中已结束。

提前致谢。

为了编写一个简单的测试class我建议您使用以下指南:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm

想法很简单:假设您在测试中创建了一个机会 class 并在您的案例中进行了插入或更新 - 您的触发器 class 将自动触发并且 运行来自 AP01_Opportunity class 的代码。你可以放一些

System.debug('some message');

检查您的逻辑是否按预期工作以及执行了哪些代码块