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
Maf_007Maf_007 

Code Coverage Issue

Hi All,

 

I have a trigger on Opportunity which updates a field in Line Item when Parent is Updated. I get 83% code coverage. Just curious to know Why not getting with the test class?? Could anyone please have a look:

 

public class OppProductClass{
    public static void OppTermUpdate(List<Opportunity> Opps){
        List <Opportunity> ChangedOpps = New List <Opportunity> ();
        List <Opportunitylineitem> OLIupdate = New List <Opportunitylineitem> ();
        List <Opportunity> tickOppsupdate = New List <Opportunity> ();
    
        For (Opportunity Opps1: Opps){
            //If(opps1.Hours_Spent_is_changed__c == TRUE || opps1.Hours_Spent_is_changed__c == False){
            Changedopps.add(Opps1);
        //}
        }
        /*List<Opportunity> tickboxupdate = [select id, Hours_Spent_Is_Changed__c from Opportunity where Hours_Spent_Is_Changed__c = True];
    
        for(Opportunity op:tickboxupdate){
            op.Hours_Spent_Is_Changed__c = false;
            tickOppsupdate.add(op);
        }*/ 
        
        List <Opportunitylineitem> OLI = [Select ID, Contract_Term__c, opportunityid, Opportunity.Contract_Term__c from Opportunitylineitem where Opportunityid in: Changedopps];
    
        For (Opportunitylineitem newoli: Oli){
            Newoli.Contract_Term__c = Newoli.Opportunity.Contract_Term__c;
            OLIupdate.add(newoli);    
        }
                
        Update Oliupdate;
        //Update tickoppsupdate;
        recursionprevent.preventflag = true;
    }
}

 Test Class:

 

/*
Author: Mahfuz Choudhury
Date: 09.10.13
ClassName:OpportunityFetcher on Opportunity
*/

@isTest(seeAllData=true) 
public class TestOppProductUpdate2{
    private static testMethod void TestOppProduct(){
       OppProductClass objclassconfirm = new OppProductClass();
        
       list <Opportunity> bulkopp = new list<Opportunity>{};
       list <OpportunityLineItem> OLIList = new list<OpportunityLineItem>{};
            
       Opportunity opp = new Opportunity (Name='test', StageName='Needs Analysis', CloseDate=system.today());
       bulkopp.add(opp);   
        
       Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
       
       Pricebook2 pbk = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
       insert pbk;
       
       Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
       insert prd;
       
       PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
       insert pbe;
        
       OpportunityLineItem OLI = new OpportunityLineItem();
       OLI.Quantity = 2;
       OLI.OpportunityId = bulkopp[0].id;
       OLI.UnitPrice = 5.00;
       OLI.PricebookEntryId = pbe.id;

       // Insert opportunity
       Test.starttest();
        try{
            OppProductClass.OppTermUpdate(bulkopp);
            insert bulkopp;
            insert OLI;
            bulkopp[0].Contract_Term__c = 15;
            OppProductClass.OppTermUpdate(bulkopp);
            Update OLI;
            Update bulkopp;
            OLI = [SELECT Quantity, Contract_Term__c FROM OpportunityLineItem WHERE Id =:bulkopp[0].Id limit 1];
            }catch(DMLexception e){
            System.assertEquals('test',bulkopp[0].Name);
            System.assertEquals(bulkopp[0].Contract_Term__c, OLI.Contract_Term__c);
            }
       Test.stopTest();

        
    }
}

 Please note, I do not get coverage for the following:

For (Opportunitylineitem newoli: Oli){
            Newoli.Contract_Term__c = Newoli.Opportunity.Contract_Term__c;
            OLIupdate.add(newoli);    
        }

 

Best Answer chosen by Admin (Salesforce Developers) 
Maf_007Maf_007

I was able to fix the issue, I was trying to put the insert opportunity in try block which was trying to bypass some fields I would have to fill in. The solution is below:

 

@isTest(seeAllData=true) 
public class TestOppProductUpdate2{
    private static testMethod void TestOppProduct(){
       OppProductClass objclassconfirm = new OppProductClass();
        
       list <Opportunity> bulkopp = new list<Opportunity>{};
       list <OpportunityLineItem> OLIList = new list<OpportunityLineItem>{};
       
       //create a new account to associate with the opportunity
        Account a = new Account(Name = 'Test Account',Account_Shortcode__c = '234');
        insert a;
            
       Opportunity opp = new Opportunity (Name='test2', Accountid = a.id, Sales_Stage__c = 'SS1 - Leads', Type = 'Renewal', Contract_Term__c = 10, 
                                          CloseDate=system.today(), Billing_Date__c = system.today(),NextStep = 'call',StageName = 'SS1 - Leads',
                                          leadsource = 'Employee Referral');
       bulkopp.add(opp);
       insert bulkopp;
        
       Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
       
       Test.starttest();
       Pricebook2 pbk = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
       insert pbk;
       
       Product2 prd = new Product2 (Name='Premium Product',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
       insert prd;
       
       PricebookEntry pbe = new PricebookEntry (Product2ID=prd.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true,UseStandardPrice=false);
       insert pbe;
        
       OpportunityLineItem OLI = new OpportunityLineItem();
       OLI.Quantity = 2;
       OLI.OpportunityId = bulkopp[0].id;
       OLI.UnitPrice = 5.00;
       OLI.PricebookEntryId = pbe.id;
       OLIList.add(OLI);
       insert OLIList;

       // Insert opportunity
        try{
            OppProductClass.OppTermUpdate(bulkopp);
            
            OLIList = [SELECT Contract_Term__c,Opportunity.Contract_Term__c FROM OpportunityLineItem WHERE Id =:bulkopp[0].Id limit 1];
            }catch(DMLexception e){
            System.assertEquals('test',bulkopp[0].Name);
            System.assertEquals(10, OLIList[0].Contract_Term__c);
            //System.assertEquals(10, OLIList[0].Opportunity.Contract_Term__c);
            }
        try{
            opp.Contract_Term__c = 15;
            OppProductClass.OppTermUpdate(bulkopp);
            update opp;
            bulkopp.add(opp);
            OLIList = [SELECT Contract_Term__c,Opportunity.Contract_Term__c FROM OpportunityLineItem WHERE Id =:bulkopp[0].Id limit 1];
        	}catch(DMLexception e){
            System.assertEquals(15, OLIList[0].Contract_Term__c);    
        	}
       Test.stopTest();

        
    }
}