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

How write test class for the following code it:-
How write test class for the following code it:-
public static void script(){
List<OpportunityLineItem> oppLine= new List<OpportunityLineItem>();
oppLine=[Select TotalPrice from OpportunityLineItem where Name like '%abc%' AND OpportunityId IN (Select id from Opportunity Where StageName='Closed Won')];
System.debug(oppLine);
Decimal tp=0;
for(OpportunityLineItem op1:oppLine){
tp=tp+op1.TotalPrice;
}
System.debug(tp);
}
I could not reach the 75% for above code.
Thanks
public static void script(){
List<OpportunityLineItem> oppLine= new List<OpportunityLineItem>();
oppLine=[Select TotalPrice from OpportunityLineItem where Name like '%abc%' AND OpportunityId IN (Select id from Opportunity Where StageName='Closed Won')];
System.debug(oppLine);
Decimal tp=0;
for(OpportunityLineItem op1:oppLine){
tp=tp+op1.TotalPrice;
}
System.debug(tp);
}
I could not reach the 75% for above code.
Thanks
Below test class works fine please this out:-
@isTest
private class ScriptTest {
@isTest static void scriptt(){
Account acc = new Account(Name = 'Test');
insert acc;
Opportunity opp = new Opportunity(Name = 'Test Opp',AccountID = acc.Id,Amount = 100,CloseDate=Date.today(),StageName='Closed Won');
insert opp;
Id pricebookId = Test.getStandardPricebookId();
Product2 prod = new Product2(
Name = 'abc',
ProductCode = '123',
isActive = true
);
insert prod;
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 10.00,
IsActive = true
);
insert pbEntry;
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = opp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = 5 * pbEntry.UnitPrice
);
insert oli;
Test.startTest();
Class_Name.script();
Test.stopTest();
}
}
}
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay
All Answers
Please try the below test class:
Change this line No 39 scriptController.script(); --> use your class Name . Method Name
Add the mandatory fields if any missing for below Account, Opportunity creations below.
Thanks,
Maharajan.C
Hi Mikasa Ackerman,
Please Find the solution of your question How write test class for the following code it:-
If You find this answer is helpful for you then please mark best answer
Thank You!
Below test class works fine please this out:-
@isTest
private class ScriptTest {
@isTest static void scriptt(){
Account acc = new Account(Name = 'Test');
insert acc;
Opportunity opp = new Opportunity(Name = 'Test Opp',AccountID = acc.Id,Amount = 100,CloseDate=Date.today(),StageName='Closed Won');
insert opp;
Id pricebookId = Test.getStandardPricebookId();
Product2 prod = new Product2(
Name = 'abc',
ProductCode = '123',
isActive = true
);
insert prod;
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId,
Product2Id = prod.Id,
UnitPrice = 10.00,
IsActive = true
);
insert pbEntry;
OpportunityLineItem oli = new OpportunityLineItem(
OpportunityId = opp.Id,
Quantity = 5,
PricebookEntryId = pbEntry.Id,
TotalPrice = 5 * pbEntry.UnitPrice
);
insert oli;
Test.startTest();
Class_Name.script();
Test.stopTest();
}
}
}
Hope this explanation will resolve your query. Mark it as the best answer if you find it helpful.
Thanks
Akshay