You need to sign in to do that
Don't have an account?
Behzad Bahadori 18
Code to be covered
how to cover if statement in unit test
id customPB = Test.getStandardPricebookId(); PricebookEntry pbe = new PricebookEntry(pricebook2id = customPB, product2id = p.id,unitprice=1.0,isActive=true); insert pbe; PricebookEntry pbe2 = new PricebookEntry(pricebook2id = customPB, product2id = p2.id,unitprice=1.0,isActive=true); insert pbe2; PricebookEntry pbe3 = new PricebookEntry(pricebook2id = customPB, product2id = p3.id,unitprice=1.0,isActive=true); insert pbe3; quote q=new quote(name='myquote',opportunityid= o1.id,pricebook2id=customPB); insert q; OpportunityLineItem oli = new OpportunityLineItem(PricebookEntryId=pbe.Id,OpportunityId=o1.Id,Quantity=10,TotalPrice=100); //QuoteLineItem testSPTqli = new QuoteLineItem(Description ='1 Year', PricebookEntryId=pbe.id, QuoteId=q.id, UnitPrice = 1, Quantity = 10); //insert testSPTqli; OpportunityLineItem oli2 = new OpportunityLineItem(PricebookEntryId=pbe2.Id,OpportunityId=o1.Id,Quantity=10,TotalPrice=100); OpportunityLineItem oli3 = new OpportunityLineItem(PricebookEntryId=pbe3.Id,OpportunityId=o1.Id,Quantity=10,TotalPrice=100); QuoteLineItem testSPTqli1 = new QuoteLineItem(Description ='2 Year', PricebookEntryId=pbe.id, QuoteId=q.id, UnitPrice = 1, Quantity = 10, Internet_Service_Fee_CheckBox__c = true , Enter_Activation__c = 10.00 ,Activation_Fee_CheckBox__c = true); insert testSPTqli1; QuoteLineItem testSPTqli2 = new QuoteLineItem(Description ='1 Year', PricebookEntryId=pbe2.id, QuoteId=q.id, UnitPrice = 1, Quantity = 10, Internet_Service_Fee_CheckBox__c = true,Enter_Activation__c = NULL ,Activation_Fee_CheckBox__c = true); insert testSPTqli2; QuoteLineItem testSPTqli3 = new QuoteLineItem(Description ='3 Year', PricebookEntryId=pbe3.id, QuoteId=q.id, UnitPrice = 1, Quantity = 10 , Internet_Service_Fee_CheckBox__c = true,Enter_Activation__c = 10.00,Activation_Fee_CheckBox__c = true); insert testSPTqli3; ApexPages.StandardController sc = new ApexPages.StandardController(q); PageReference pageRef = Page.QuotePDF; pageRef.getParameters().put('id', String.valueOf(q.Id)); Test.setCurrentPage(pageRef); OppExt oppExt = new OppExt(sc); Integer intSvcTerm; intSvcTerm = 12; Integer voiceSvcTerm; voiceSvcTerm = 0; Test.stopTest();
Code to be covered
if (qli.Enter_Activation__c != NULL ){ qli.Activation_Fee_CheckBox__c = true; } voiceHwLineItems.add(qli); System.debug('I am the' + qli); } //add the voice related ones to the voice service section else{ //only save the term if this is for the main voice service if(voiceSvcTerm == 0){ if(qli.Description.contains('1 Year')) voiceSvcTerm = 12; else if(qli.Description.contains('2 Year')) voiceSvcTerm = 24; else if(qli.Description.contains('3 Year')) voiceSvcTerm = 36; System.debug('Setting Internet Term to ' + voiceSvcTerm); } if(voiceSvcTerm == 0 && prd.ProductCode == 'Service' && prd.Term__c != null && qli.Description.contains('Extension')){ System.debug('Setting Voice Term to ' + prd.Term__c); voiceSvcTerm = Integer.valueOf(prd.Term__c); } if(prd.ProductCode == 'Service' && voiceSvcLineItems.size() > 0){ if (qli.Enter_Activation__c != NULL ){ qli.Activation_Fee_CheckBox__c = true; } voiceSvcLineItems.add(0, qli); } else voiceSvcLineItems.add(qli); } } }
However, let me try giving you some suggestions with the following assumptions for your code...
Assumptions and corresponding Test class soutions:
Assumption 1: The piece of code you have given is inside a Method lets say for example processqli() in your controller class.
Assumption 2: if Opportunity object and its related Quote, Quote Line Item (in your code qli) is defined as a class variable and accessed in the method processqli.... like below..
Assumption 3: if you are processing the Quote Line items in a loop inside your method.. the missing code before line no .. 01 if (qli.Enter_Activation__c != NULL ){
Public Class yourController{
public Opportunity opp ;
public Product2 prd;
public Integer voiceSvcTerm;
Public Pagereference processqli() {
for(
... your code
}
}
Then the Test code for the above should be like ...
Add the following after line no 36 in your test class
// Pass the controller variable values from test class
sc.voiceSvcTerm = voiceSvcTerm; // Pass the class variable voiceSvcTerm value
sc.opp = o1; // Pass the Opportunity you created in test class to controller instance 'sc'
sc.prd = p1; // Process product you created in test classto controller instance 'sc '
sc.processqli(); // Call the method in which your code logic is kept ( i.e. in my example its processqli)
you can repeat the above lines of code with differnet Opportunities and Products you created in test class to get the code coverage for each if and else conditions in your controller...
Assumption 4: if you are querying the Opportunity and the related onbjects Quote, Quote Line Items in your method instead of class variables then the test code is to just call the class method like below....
add the following after line no 36 in your test class
sc.processqli();
Try the above suggestions based on your code structure...
or share the complete method or the whole controller to give you a close solution...
Good Luck!
- Nethaji