My test class:
@isTest
private class opplineitemHandlerTest {
@isTest
static void testCalculateTotal() {
// Create test data
Pricebook2 standardPricebook = [SELECT Id,Name FROM Pricebook2 WHERE IsStandard = true];
//Pricebook2 standardPricebook = new Pricebook2();
system.debug(standardPricebook);
Product2 testProduct = new Product2(Name = 'Test Product', IsActive = true);
insert testProduct;
PricebookEntry standardPricebookEntry = new PricebookEntry(Pricebook2Id = standardPricebook.Id, Product2Id = testProduct.Id, UnitPrice = 100, IsActive = true);
insert standardPricebookEntry;
Opportunity testOpp = new Opportunity(Name = 'Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today());
insert testOpp;
OpportunityLineItem testLineItem1 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 1, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
OpportunityLineItem testLineItem2 = new OpportunityLineItem(OpportunityId = testOpp.Id, Quantity = 2, TotalPrice = 100, PricebookEntryId = standardPricebookEntry.Id);
List<OpportunityLineItem> testLineItems = new List<OpportunityLineItem>{testLineItem1, testLineItem2};
insert testLineItems;
// Call the method to be tested
Test.startTest();
opplineitemHandler.calculateTotal(testLineItems);
Test.stopTest();
// Verify the results
Opportunity updatedOpp = [SELECT Id, Total_Line_Item_Value__c FROM Opportunity WHERE Id = :testOpp.Id];
System.assertEquals(400, updatedOpp.Total_Line_Item_Value__c);
}
}
I'm getting an error like 'List has no rows for assignment to SObject' on the Pricebook2 SOQL query. Could you please help me resolve this?
@TestSetup
method. (Or you use@SeeAllData
on the test class, but that is an anti-pattern.)