You need to sign in to do that
Don't have an account?
Creating Test Data for Opportunties
Hi everyone,
I do a lot of work that involves the Opportunity object, along with the attached Price Book, Product, and Opportunity Product objects.
Because these objects and their relationships can get kind of complicated (especially creating standard and custom Price Book entries for each Product before creating an Opportunity Product), I want to create an Apex Class that will generate this data when needed. That way I can call the necessary data whenever I run a new test, instead of adding that code into each new Apex test class I create.
This is what I have so far, but I'm unsure if I'm on the right track:
@isTest public class OpportunityDataGenerator { // create test data @testSetup public static void createOpportunityData() { // create standard price book Pricebook2 stdPricebook = new Pricebook2( Name = 'Standard Price Book', IsActive = true ); insert stdPricebook; // create custom price book Pricebook2 customPricebook = new Pricebook2( Name = 'Custom Price Book', IsActive = true ); insert customPricebook; // create product with standard price book and custom price book entries Product2 product = new Product2( Name = 'Test Product', ProductCode = 'TEST001', IsActive = true ); insert product; // add standard price book entry for the product PricebookEntry stdPricebookEntry = new PricebookEntry( Pricebook2Id = stdPricebook.Id, Product2Id = product.Id, UnitPrice = 100.0, IsActive = true ); insert stdPricebookEntry; // add custom price book entry for the product PricebookEntry customPricebookEntry = new PricebookEntry( Pricebook2Id = customPricebook.Id, Product2Id = product.Id, UnitPrice = 150.0, IsActive = true ); insert customPricebookEntry; // create account with Client_Type__c field Account account = new Account( Name = 'Test Account', Client_Type__c = 'Associate' ); insert account; // create opportunity related to the account Opportunity opportunity = new Opportunity( Name = 'Test Opportunity', StageName = 'Prospecting', CloseDate = Date.today(), AccountId = account.Id ); insert opportunity; // create opportunity product related to the opportunity and the product OpportunityLineItem opportunityProduct = new OpportunityLineItem( OpportunityId = opportunity.Id, PricebookEntryId = customPricebookEntry.Id, Quantity = 10, UnitPrice = customPricebookEntry.UnitPrice ); insert opportunityProduct; } }
The test class provided looks good and creates the necessary data to test functionality related to the Opportunity, Pricebook2, Product2, and OpportunityLineItem objects. However, there are a few minor changes that can be made for optimization and readability:
Add comments: While the current code has comments for each section, it's best practice to add comments for each line to explain what the code is doing.
Use constants: To avoid hard-coding IDs or values, it's best to use constants that are defined at the beginning of the class. This makes it easier to update values if needed and reduces the likelihood of errors.
Use try-catch blocks: To handle any exceptions that may occur during data creation, it's best to wrap each insert statement in a try-catch block.
Here's an updated version of the class that implements these changes:
If you find my answer helpful, please mark it as the best answer. Thanks!