You need to sign in to do that
Don't have an account?

Can't get test coverage where am i going wrong? Apex Trigger
Hi Dev community, I have written an apex trigger to automatically convert an opportunity product to an asset dependent on criteria, I have managed to write a test class but I think I am missing something simple to get coverage. As it stands I only get 9% coverage on my trigger.
Apex Trigger
trigger OpportunityAssetonClosedWon on Opportunity (after insert, after update) { for(Opportunity o: trigger.new){ if(o.isWon==true && o.HasOpportunityLineItem==true){ String opptyId = o.Id; OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id, Product2.Name, Product2.Family, PricebookEntry.Product2.Name From OpportunityLineItem WHERE OpportunityId=:opptyId]; Asset[] ast = new Asset[]{}; Asset a = new Asset(); for(OpportunityLineItem ol: OLI){ if(ol.Product2.Family=='Terminal' || ol.Product2.Family=='Gateway' ){ a = new Asset(); a.AccountId = o.AccountId; a.Product2Id= ol.PricebookEntry.Product2Id; a.Quantity= 1; a.Price= ol.UnitPrice; a.PurchaseDate=o.CloseDate; a.status='Purchased'; a.Name = ol.Product2.Name; ast.add(a); ol.Converted_to_Asset__c = true; } } update OLI; insert ast; } } }
Apex Test Class
@isTest public class OpportunityAssetonClosedWonTest { static testMethod void closedOpportunity() { Account testAcc = new Account(Name = 'TestAccount'); insert testAcc; Pricebook2 p = new Pricebook2(Name = 'Testbook'); insert p; Opportunity testOpportunity = new Opportunity( StageName = 'Sourcing Demand', CloseDate = Date.newInstance(2017,12,31), AccountId = testAcc.Id, Name = 'Test Opportunity Triggers', Pricebook2Id=Test.getStandardPricebookId() ); insert testOpportunity; Pricebook2 pb22 = new Pricebook2(Name='testDIE'); insert pb22; Product2 pro1 = new Product2(Name='BXAD', isActive=true ,Family = 'Terminal'); insert pro1; Product2 pro2 = new Product2(Name='BXCD', isActive=true ,Family = 'Gateway'); insert pro2; Product2 pro3 = new Product2(Name='BXBD', isActive=true ,Family = 'Card Not Present'); insert pro3; PricebookEntry pbe1 =new PricebookEntry(UnitPrice=10000,Product2Id=pro1.Id,Pricebook2Id=Test.getStandardPricebookId(), isActive=true,UseStandardPrice = false); insert pbe1; PricebookEntry pbe2 =new PricebookEntry(UnitPrice=5000,Product2Id=pro2.Id,Pricebook2Id=Test.getStandardPricebookId(), isActive=true,UseStandardPrice = false); insert pbe2; PricebookEntry pbe3 =new PricebookEntry(UnitPrice=1000,Product2Id=pro3.Id,Pricebook2Id=Test.getStandardPricebookId(), isActive=true,UseStandardPrice = false); insert pbe3; OpportunityLineItem OPplineitem1 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe1.Id); insert OPplineitem1; OpportunityLineItem OPplineitem2 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe2.Id); insert OPplineitem2; OpportunityLineItem OPplineitem3 = new OpportunityLineItem (Quantity=1, OpportunityId=testOpportunity.Id,UnitPrice=0.01,PriceBookEntryId=pbe3.Id); insert OPplineitem3; Test.startTest(); testOpportunity.StageName = 'Closed/Won'; update testOpportunity; Test.stopTest(); System.assertEquals(testOpportunity.StageName, 'Closed/Won'); } }
Try the below code it will help you to increase the code coverage of your trigger : Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
Regards,
Akshay
All Answers
If those two variables, isWon and HasOpportunityLineItem, are formula fields or relying on a workflow rule to populate the value, then you should query for your Opportunity to use when testing in the Test.startTest()/Test.stopTest() block of code so that the rule or formula has had a chance to run.
Try the below code it will help you to increase the code coverage of your trigger : Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
Regards,
Akshay
Thanks again by the way
Thanks for selecting my answer as best answer.
I have just made one single change in my test class on line number 11, where I have asigned StageName='Closed Won'.
In your Apex Trigger you are checking that "if(o.isWon==true && o.HasOpportunityLineItem==true)" and "isWon" Condition will be true only when the StageName of the Opportunity is "Closed Won".
What you were doing in your test class at line number 13, where you have assigned StageName='Sourcing Demand' so "isWon" condition will never be true and it will never go inside the if condition that's why your maximum code is not covered.
Hope I clear your doubt.
Regards,
Akshay