You need to sign in to do that
Don't have an account?
Maf_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); }
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: