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
Del SantosDel Santos 

Test class keeps failing

Hi Guys!

Good day. I am creating a test method for a class i just created from a recycled apex code.
I have a requirement of updating the custom lookup field product__c in our opportunity object.
This field gets its value from the opportunity line item.  

I've created an apex class, got the code somewhere from the internet, i read and tried a lot, fortunately made it working for our requirement. :)
The problem is i been trying to make a test method for it but keeps failing. I am a new admin and do not know much about coding.

Can someone help me to make a test class please. :) TIA guys!

Heres my apex class:

public class UpdateOLIProdMgr {
    public static void updateOppProdMgr(OpportunityLineItem[] oppLineItems)
        {
            List<Opportunity> opps = new List<Opportunity>();
            List<Id> idPriceBookEntry = new List<Id>();
            List<Id> idProducts = new List<Id>();
            List<String> strFamily = new List<String>();
             

            for(OpportunityLineItem oppLineItem:oppLineItems)
            {
                idPriceBookEntry.add(oppLineItem.PricebookEntryId);             
            }   
            
            
            List<PricebookEntry> sObjPriceBookEntries = [Select Product2Id, Product2.Family, Product2.Name from PricebookEntry where Id in :idPriceBookEntry]; 
            for(PricebookEntry objPriceBookEntry:sObjPriceBookEntries)
            {
                idProducts.add(objPriceBookEntry.Product2Id);
                strFamily.add(objPriceBookEntry.Product2.Family);
            }
            
                        
                       
            if (sObjPriceBookEntries.size() > 0)
            {
                                 
                Integer intCounter = 0;
                for(OpportunityLineItem oppLineItem:oppLineItems)
                {
                    Opportunity opp = new Opportunity(id = oppLineItem.OpportunityId,Product__c = idProducts[intCounter]);                                                                                                       
                    opps.add(opp);
                    intCounter++;
                }
             
                Database.update(opps);  
            }
                                
        }
    
    public static void updateOLIMgr(OpportunityLineItem[] oppLineItems)
    {
        List<Id> idOpp = new List<Id>();
               
        for(OpportunityLineItem oppLI: oppLineItems)
        {
            idOpp.add(oppLI.OpportunityId);
        }       
        
         
        
    }   
    
    public static void eraseOppProd(OpportunityLineItem[] oppLineItems)
    {
        List<Opportunity> opps = new List<Opportunity>();
        List<Id> idOpp = new List<Id>();
            
        for(OpportunityLineItem oppLI: oppLineItems)
        {
            idOpp.add(oppLI.OpportunityId);
        }       
        
        List<Opportunity> sObjOpp = [Select Id, Product__c from Opportunity where Id in :idOpp];
        
        for(Opportunity opp:sObjOpp)
            {
                Opportunity oppty = new Opportunity(id=opp.id, Product__c = null, X50__c = null);                                                                                                        
                opps.add(oppty);
            }
        database.update(opps);
    }
  }
Best Answer chosen by Del Santos
LBKLBK
Here is your Test Class.
 
@istest
public class testUpdateOLIProdMgr {
    static testmethod void testInsert(){
    
        test.startTest();
        
        Account a = new Account();
        a.name = 'test';
        a.Phone='235-345-4564';
        insert a; 
        
        Contact con = new Contact();
        con.firstName = 'test';
        con.lastName = 'test';
        con.accountId = a.Id;
        insert con;
        
        
        id pricebookId = Test.getStandardPricebookId();
        
        Product2 p2 = new Product2();
        p2.name = 'test';
        p2.Family = 'License';
        p2.IsActive =true;
        insert p2;
        
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = p2.Id, UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = p2.Id, UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        Opportunity o = new Opportunity();
        o.name = 'test';
        o.stageName = '07 - Closed Won';
        o.accountId = a.Id;
       // system.debug('Inside create oppt Pid is as:'+ pid);
        o.Pricebook2id=customPB.Id;
        o.closeDate = system.today().adddays(30);
        o.LeadSource = 'Deal Registration';
        insert o;   
        
        OpportunityContactRole ocr = new OpportunityContactRole();
        ocr.opportunityId = o.Id;
        ocr.contactId = con.Id;
        insert ocr;
        
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.opportunityId = o.id;
        oli.PricebookentryId = customPrice.Id;
        oli.TotalPrice = 500;
        oli.Quantity = 10;
        insert oli;
        List<OpportunityLineItem> lstOLI = new List<OpportunityLineItem>();
        lstOLI.add(oli);
        
        UpdateOLIProdMgr.updateOppProdMgr(lstOLI);
        UpdateOLIProdMgr.updateOLIMgr(lstOLI);
        UpdateOLIProdMgr.eraseOppProd(lstOLI);
        test.stopTest();
    }
    
}

 

All Answers

LBKLBK
Here is your Test Class.
 
@istest
public class testUpdateOLIProdMgr {
    static testmethod void testInsert(){
    
        test.startTest();
        
        Account a = new Account();
        a.name = 'test';
        a.Phone='235-345-4564';
        insert a; 
        
        Contact con = new Contact();
        con.firstName = 'test';
        con.lastName = 'test';
        con.accountId = a.Id;
        insert con;
        
        
        id pricebookId = Test.getStandardPricebookId();
        
        Product2 p2 = new Product2();
        p2.name = 'test';
        p2.Family = 'License';
        p2.IsActive =true;
        insert p2;
        
        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = pricebookId, Product2Id = p2.Id, UnitPrice = 10000, IsActive = true);
        insert standardPrice;
        
        Pricebook2 customPB = new Pricebook2(Name='Custom Pricebook', isActive=true);
        insert customPB;
        
        PricebookEntry customPrice = new PricebookEntry(Pricebook2Id = customPB.Id, Product2Id = p2.Id, UnitPrice = 12000, IsActive = true);
        insert customPrice;
        
        Opportunity o = new Opportunity();
        o.name = 'test';
        o.stageName = '07 - Closed Won';
        o.accountId = a.Id;
       // system.debug('Inside create oppt Pid is as:'+ pid);
        o.Pricebook2id=customPB.Id;
        o.closeDate = system.today().adddays(30);
        o.LeadSource = 'Deal Registration';
        insert o;   
        
        OpportunityContactRole ocr = new OpportunityContactRole();
        ocr.opportunityId = o.Id;
        ocr.contactId = con.Id;
        insert ocr;
        
        
        OpportunityLineItem oli = new OpportunityLineItem();
        oli.opportunityId = o.id;
        oli.PricebookentryId = customPrice.Id;
        oli.TotalPrice = 500;
        oli.Quantity = 10;
        insert oli;
        List<OpportunityLineItem> lstOLI = new List<OpportunityLineItem>();
        lstOLI.add(oli);
        
        UpdateOLIProdMgr.updateOppProdMgr(lstOLI);
        UpdateOLIProdMgr.updateOLIMgr(lstOLI);
        UpdateOLIProdMgr.eraseOppProd(lstOLI);
        test.stopTest();
    }
    
}

 
This was selected as the best answer
Del SantosDel Santos
Hi LBK! 
This is great!
User-added image


Thanks a lot! I owe you one. I shall be reviewing this code so i can make one..ty!