+ Start a Discussion
Sandra WicketSandra Wicket 

TestClass for so query

Hi there,

i want to test following methode: 
public List <oppProduct> getOli (){
        if(productList ==null){
            productList = new List <oppProduct>();
            for(OpportunityLineitem oli : 
                [select Id, Quantity, Produkt__c, UnitPrice , product_family__c, Product2.Name, ON_Produktbeschreibung_detailliert__c From OpportunityLineitem 
                        WHERE Opportunity.Id = :opportunityId
                        AND product_family__c = 'Extra Boost']){
                productList.add(new oppProduct(oli));   
        
            }   
        }
        return productList; 
    }

    
In my test class i created these test records :

 @isTest static void testGetOli () {
        
        List<CreateMultiExtraBoost.oppProduct> productListTest = new List <CreateMultiExtraBoost.oppProduct>(); 
 
        Id pricebookId = Test.getStandardPricebookId();
        
        Product2 pro = new Product2();
        pro.name = 'test';
        pro.Produktbeschreibung_detailliert__c = 'test';
        pro.Family = 'Extra Boost'; 
        pro.IsActive = true;
        insert pro;
        
        Product2 pro1 = new Product2();
        pro1.name = 'test';
        pro1.Produktbeschreibung_detailliert__c = 'test';
        pro1.Family = 'Sonstiges'; 
        pro1.IsActive = true;
        insert pro1;
        
        PricebookEntry pEntry = new PricebookEntry (); 
        pEntry.Product2Id = pro.id;
        pEntry.UseStandardPrice = false;
        pEntry.UnitPrice = 600; 
        pEntry.Pricebook2Id = pricebookId;
        pEntry.IsActive = true;
        insert pEntry;
        
        PricebookEntry pEntry1 = new PricebookEntry (); 
        pEntry1.Product2Id = pro1.id;
        pEntry1.UseStandardPrice = false;
        pEntry1.UnitPrice = 300; 
        pEntry1.Pricebook2Id = pricebookId;
        pEntry1.IsActive = true;
        insert pEntry1;
        
        Opportunity opp = new Opportunity ();
        opp.name = 'test';
        opp.StageName = '40 - Demo Termin vereinbart';
        opp.Override_Region__c = 'München';
        opp.CloseDate = System.today();
        insert opp;
        
           OpportunityLineItem oli = new OpportunityLineItem ();
        oli.TotalPrice = 500;
        oli.Quantity = 4;
        oli.OpportunityId = opp.Id;
        oli.Product2Id = pro.id;
        oli.PricebookEntryId = pEntry.id;
        insert oli;
        
        OpportunityLineItem oli1 = new OpportunityLineItem ();
        oli1.TotalPrice = 500;
        oli1.Quantity = 4;
        oli1.OpportunityId = opp.Id;
        oli1.Product2Id = pro1.id;
        oli1.PricebookEntryId = pEntry1.id;
        insert oli1;
        
        Test.StartTest();
        CreateMultiExtraBoost cme = new CreateMultiExtraBoost();
        cme.getOli();
        Test.StopTest();

i am unable to cover the product_family__c field. It is a formula field, which is related to the product object. Thanks for your help guys ! 
 
Shamsi 110Shamsi 110
I think the issue is with this section Opportunity.Id = :opportunityId.
is this a method are being called from trigger ?

Make sure this Opportunity.Id = :opportunityId is TRUE.
Sandra WicketSandra Wicket
hi Shamsi, 
thanks for your reply. 
the methode is called from a visualforcepage. I have checked the opportunityId. The opportunity line item got the same id.   These two lines are red in my test file :

AND product_family__c = 'Extra Boost']){                
productList.add(new oppProduct(oli));