You need to sign in to do that
Don't have an account?
Need Help Increasing Test Coverage
Please Help
Hi I have a apex classes and custom object My Class that creates an Opportunity and Line Items Records when saved on My Visualforce Page. I created a test class to deploy into prod, but can only get 64%coverage. Can someone help me increase my test coverage or guide me in the right dirrection so I can get as close to 100% Also when I run the test I get one Test Failure : System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Name, StageName, CloseDate]: [Name, StageName, CloseDate]
Below is my Test Class:
@isTest private class TestProInterestToOpportunity { static testMethod void myTest() { List<Customer_Visited__c> lstOpp = new List<Customer_Visited__c>(); ApexPages.StandardSetController sc = new ApexPages.StandardSetController(lstOpp); ProInterestToOpportunity myCls = new ProInterestToOpportunity (sc); //Create object reference of 'LeadConvert' Account objAcc= new Account(Name='Test',Phone='0841545441',Fax='029295789'); insert objAcc; Customer_Visit_Report__c objCusRpt = new Customer_Visit_Report__c(); objCusRpt.Name = 'test'; insert objCusRpt; Customer_Visited__c objCusv = new Customer_Visited__c(Customer_Visit_ReportId__c=objCusRpt.Id, AccountId__c= objAcc.Id); insert objCusv; ApexPages.CurrentPage().getParameters().put('Id',objCusv.Id); Product2 objProduct = new product2(name='unittest'); insert objProduct; Pricebook2 stdPb = [select Id from Pricebook2 where isStandard=true limit 1]; insert new PricebookEntry(pricebook2id = stdPb.id, product2id = objProduct.id,unitprice=1.0, isActive=true); // Next, it creates a new pricebook with an entry for this product Pricebook2 PB = new pricebook2(name='unittest'); insert PB; PricebookEntry PE = new PricebookEntry(pricebook2id=PB.id, product2id=objProduct.id,unitprice=1.0, isActive=true); insert PE; Product_of_Interest__c objLineItem = new Product_of_Interest__c(); objLineItem = new Product_of_Interest__c(); objLineItem.Customer_VisitedID__c = objCusv.Id; objLineItem.Customer_Visit_ReportID__c = objCusv.Customer_Visit_ReportID__c; objLineItem.ProductId__c = objProduct.Id; objLineItem.PriceBookEntryId__c = PE.Id; objLineItem.Name = objProduct.Name; objLineItem.Status__c = 'Not Started'; insert objLineItem; Opportunity objOpp = new Opportunity(); objOpp.Name = objCusv.AccountName__c + '-'; objOpp.AccountID = objCusv.AccountID__c; objOpp.CloseDate = System.Today(); objOpp.StageName = 'Prospecting'; insert objOpp; //ProInterestToOpportunity.CusvWrapper lw = myCls.objCusvW; myCls.objOpp = myCls.getFillOpportunity(); PageReference objPageRef = myCls.ConvertCurrentOpp(); objPageRef = myCls.Cancel(); myCls.lstCusv= myCls.getlstCusv(); } }
Below is my Apex Class:
public class ProInterestToOpportunity { public ProInterestToOpportunity(ApexPages.StandardController controller) { } public ProInterestToOpportunity(ApexPages.StandardSetController controller) { controller.setPageSize(10); } public boolean IsConverted {get;set;} Id oppId; public string OpportunityStage {get;set;} public List<Customer_Visited__c> lstCusv; public Opportunity objOpp {get{if (objOpp == null) objOpp = new Opportunity(); return objOpp;} set;} public List<SelectOption> getStageName() { OpportunityStage ls = [select MASTERLABEL from OpportunityStage where IsWon = False]; OpportunityStage = ls.MASTERLABEL; List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption(ls.MASTERLABEL,ls.MASTERLABEL)); return options; } public List<Customer_Visited__c> getlstCusv() { if (lstCusv== null){ return [select AccountName__c,AccountId__c,Customer_Visit_ReportID__c,LastModifiedDate from Customer_Visited__c where id =: ApexPages.currentPage().getParameters().get('id') and AccountId__c != null]; } else return lstCusv; } public Opportunity getFillOpportunity() { if(lstCusv != null){ for (Customer_Visited__c lc: lstCusv) { objOpp.Name = lc.AccountName__c + '-'; objOpp.StageName = OpportunityStage; objOpp.CloseDate= System.Today(); objOpp.AccountId = lc.AccountID__c; } } return objOpp; } public PageReference ConvertCurrentOpp() { List<Opportunity> lstOpp = [select Id,Name,AccountId,CloseDate,StageName from Opportunity where Name=:objOpp.Name]; if (lstOpp.size() > 0) { lstOpp[0].Name =objOpp.Name; lstOpp[0].StageName = objOpp.StageName; lstOpp[0].CloseDate = objOpp.CloseDate; lstOpp[0].AccountId = objOpp.AccountId; update lstOpp[0]; oppId = lstOpp[0].Id; }else{ insert objOpp; oppId = objOpp.Id; } if (lstCusv.size() > 0) { lstCusv[0].IsConverted__c = true; update lstCusv; } //Move all leadlineitems to opportunitylineitems List<Product_of_Interest__c> lstProIn = [select id,Average_Price__c,Description__c,ProductID__c,Material_Packing_Group__c,Opportunity__c,PriceBookEntryID__c from Product_of_Interest__c where Customer_VisitedID__c = :ApexPages.currentPage().getParameters().get('Id')]; if (lstProIn.size() > 0 && objOpp.Id != null) { List<OpportunityLineItem > lstOppTeam = new List<OpportunityLineItem >(); for(Product_of_Interest__c l:lstProIn) { if(l.Opportunity__c != null || l.Opportunity__c != 0){ OpportunityLineItem LineItem = new OpportunityLineItem(); LineItem.OpportunityId = objOpp.Id; LineItem.Description = l.Description__c; LineItem.Material_Packing_Group__c = l.Material_Packing_Group__c; LineItem.PricebookEntryId = l.PriceBookEntryID__c; LineItem.Quantity = l.Opportunity__c; LineItem.UnitPrice = l.Average_Price__c; LineItem.TotalPrice *= l.Opportunity__c; lstOppTeam.add(LineItem); } } insert lstOppTeam; } PageReference returnPage = new PageReference('/' + oppId); returnPage.setRedirect(true); return returnPage; } public PageReference Cancel() { PageReference returnPage = new PageReference('/' + ApexPages.currentPage().getParameters().get('id')); returnPage.setRedirect(true); return returnPage; }
Below is my Visual Force Page:
<apex:page id="CustomerToOpportunity" standardController="Customer_Visited__c" extensions="ProInterestToOpportunity"> <apex:form id="CloseOpportunitiesForm"> <apex:pageBlock id="pageBlock" mode="edit"> <apex:pageBlockButtons > <apex:commandbutton action="{!ConvertCurrentOpp}" value="Closed" /> <apex:commandbutton action="{!Cancel}" value="Cancel" /> </apex:pageBlockButtons> <apex:pageBlockSection id="OpportunityNew" collapsible="false" title="New Opportunities" columns="1"> <apex:pageblockSectionItem id="OppBlockSectionItem"> Opportunity Name <apex:outputPanel > <div class="requiredInput"> <div class="requiredBlock"></div> <apex:inputField id="txtOpportunity" value="{!FillOpportunity.Name}" required="true" /> </div> </apex:outputPanel> </apex:pageblockSectionItem> <apex:pageblockSectionItem id="StageBlockSectionItem"> Stage <apex:selectList value="OpportunityStage"> <apex:inputField id="Stage" value="{!StageName}" required="true" /> </apex:selectList> </apex:pageblockSectionItem> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Definitly there is scope for coverage inprovement in your test class but first we need to fix the failure,
In your class you are inserting oppurtunity in this part
please check when else part gets executed are req fields Name, StageName, CloseDate are there, I think they are null. If you will resolve failure your code coverage will also increase. Let me know if any issue in it.