You need to sign in to do that
Don't have an account?
Test class coverage issue - Not covering for loops
Hi All,
When i run the test class, it covers only 50% not covering the For loop condition , can you pls help me how to cover this :
Apex class:
public class SortedLineItemsController {
public Opportunity opportunity { get; set; }
public OpportunityLineItem[] getSorted() {
if (opportunity == null || opportunity.opportunityLineItems== null) {
return null;
}
// TODO: Add your sorting logic here (demo just reverses the list)
OpportunityLineItem[] result = new OpportunityLineItem[1];
for (OpportunityLineItem item : opportunity.opportunityLineItems) {
result.add(0, item);
}
return result;
}
}
Test class :
@isTest
public class SortedLineItemsController_Test {
@isTest static void insertOpp() {
Id pricebookId = Test.getStandardPricebookId();
Test.startTest();
// insert product
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
insert p;
// insert pricebook entry for the product
PricebookEntry standardPrice = new PricebookEntry();
standardPrice.Pricebook2Id = Test.getStandardPricebookId();
standardPrice.Product2Id = p.Id;
standardPrice.UnitPrice = 100;
standardPrice.IsActive = true;
standardPrice.UseStandardPrice = false;
insert standardPrice ;
Account testAccount1 = new Account();
testAccount1.Name = 'Test Account 1';
testAccount1.Industry__c = 'Aerospace';
insert testAccount1;
Opportunity testOpportunity1 = new Opportunity();
testOpportunity1.Name = 'Test Opportunity';
testOpportunity1.AccountId = testAccount1.Id;
testOpportunity1.StageName = '0 - Qualify Opportunity';
testOpportunity1.Probability_2__c = '10%' ;
testOpportunity1.CloseDate = system.today() + 10;
testOpportunity1.Pricebook2Id = Test.getStandardPricebookId();
insert testOpportunity1;
Opportunity testOpportunity2 = new Opportunity();
testOpportunity2.Name = 'Test Opportunity';
testOpportunity2.AccountId = testAccount1.Id;
testOpportunity2.StageName = '0 - Qualify Opportunity';
testOpportunity2.Probability_2__c = '10%' ;
testOpportunity2.CloseDate = system.today() + 10;
testOpportunity2.Pricebook2Id = Test.getStandardPricebookId();
insert testOpportunity2;
// Add product and Pricebook to the particular opportunity using OpportunityLineItem
OpportunityLineItem oppLineItem = new OpportunityLineItem();
oppLineItem.OpportunityId = testOpportunity1.Id;
oppLineItem.PricebookEntryId = standardPrice.Id;
oppLineItem.UnitPrice = 7000.00;
oppLineItem.Quantity = 5;
oppLineItem.Amount_Updated__c = True;
oppLineItem.Service_Percentage__c = 10;
insert oppLineItem;
// Add product and Pricebook to the particular opportunity using OpportunityLineItem
OpportunityLineItem oppLineItem1 = new OpportunityLineItem();
oppLineItem1.OpportunityId = testOpportunity2.Id;
oppLineItem1.PricebookEntryId = standardPrice.Id;
oppLineItem1.UnitPrice = 6000.25;
oppLineItem1.Quantity = 5;
oppLineItem1.Amount_Updated__c = True;
oppLineItem1.Service_Percentage__c = 15;
insert oppLineItem1;
SortedLineItemsController slic = new SortedLineItemsController();
List<OpportunityLineItem> slic_q = slic.getSorted();
OpportunityLineItem[] slic_qli = slic.getSorted();
OpportunityLineItem[] slic_qli1 = slic.getSorted();
Test.stopTest();
}
}
can you pls check and let me know how to cover ...
Thanks in Advance
Try below Code Please Mark It As Best Answer If It Helps
Thank You!
All Answers
Try below Code Please Mark It As Best Answer If It Helps
Thank You!
Now its cover upto 75% ...
Do you have any how to cover the 'Return' statment
****return result****
Thanks