Unit Test Data Factories & Helpers

Unit Test Data Factories & Helpers

Available in: Enxoo Commerce, Enxoo for Communications

This article outlines package classes that are helpful when scripting unit test for functionalities dependent on Enxoo packages.

It’s quite frequent that as part of your implementation you will extend one of Enxoo objects with your own code and eventually you will need to write Unit Tests to cover this functionality. Enxoo packages have some global utilities that might help you with that.

Test Utils

enxCPQ.CPQ_TestUtils and enxB2B.B2B_TestUtils are two classes that provide global factory methods that allow you to easily create data for your unit tests. You can use these methods very easily:

// To create Account with some default values generated by the package Account account = enxCPQ.CPQ_TestUtils.createAccount(new Account(), true); // To create Account with desired Name Account account = enxCPQ.CPQ_TestUtils.createAccount(new Account(Name = 'TEST'), true); // To create 5 QuoteLineItems // Please note that CPQ_TestUtils method is creating all required objects including: // Product, PricebookEntry, Account, Opportunity, Quote automatically for you!) QuoteLineItem qli = enxCPQ.CPQ_TestUtils.createQuoteLineItems(new QuoteLineItem(), 5, true);

The methods exposed in TestUtils classes are very similar, they usually have the following parameters:

  • SObject template - object template allowing you to pre-set some field values by yourself

  • Integer count - number of records to be created (if m

  • Boolean isInsert - if set to true - records will be inserted to database

Sample Code

In a sample scenario we created an APEX trigger on Contract object that is supposed to be executed

In order to quickly set-up data for test we in a testSetup method you can leverage Enxoo package factories:

@TestSetup static void makeData(){ Account directCustomerAccount = enxCPQ.CPQ_TestUtils.createAccount(new Account(Name = 'Direct Engergie'), true); Contact directCustomerContact = enxCPQ.CPQ_TestUtils.createContact(new Contact(AccountId = directCustomerAccount.Id, FirstName = 'TestFirstName', LastName = 'TestLastName', Email = 'test' + String.valueOf(Math.random()*10000000).left(6) + '@dummyemailaddress.com'), true); Contract directCustomerContract = enxCPQ.CPQ_TestUtils.createContract(new Contract(AccountId = directCustomerAccount.Id, Contract_Type__c = CTR_Utils.CTR_TYPE_CUSTOMER), true); Contract resellerContract = enxCPQ.CPQ_TestUtils.createContract(new Contract(AccountId = directCustomerAccount.Id, Contract_Type__c = CTR_Utils.CTR_TYPE_RESELLER), true); Account indirectCustomerAccount = enxCPQ.CPQ_TestUtils.createAccount(new Account(Name = 'Indirect Communications'), true); Contact indirectCustomerContact = enxCPQ.CPQ_TestUtils.createContact(new Contact(AccountId = indirectCustomerAccount.Id, FirstName = 'TestFirstName', LastName = 'TestLastName'), true); Contract indirectCustomerContract = enxCPQ.CPQ_TestUtils.createContract(new Contract(AccountId = indirectCustomerAccount.Id, Contract_Type__c = CTR_Utils.CTR_TYPE_INDIRECT, Reseller__c = directCustomerAccount.Id), true); }

Available Global Methods

You can also check for available method signatures from Setup → Apex Classes → desired class as well as by opening it in Developer Console:

CPQ_TestUtils

static Account createAccount(Account accountTemplate, Boolean isInsert) static enxCPQ__AccountPricebook__c createAccountPricebook(enxCPQ__AccountPricebook__c accountPricebookTemplate, Boolean isInsert) static List createAccountPricebooks(enxCPQ__AccountPricebook__c accountPricebookTemplate, Integer count, Boolean isInsert) static List createAccounts(Account accountTemplate, Integer count, Boolean isInsert) static enxCPQ__ApprovalRule__c createApprovalRule(enxCPQ__ApprovalRule__c approvalRuleTemplate, Boolean isInsert) static enxCPQ__ApprovalRuleCondition__c createApprovalRuleCondition(enxCPQ__ApprovalRuleCondition__c approvalRuleConditionTemplate, Boolean isInsert) static List createApprovalRuleConditions(enxCPQ__ApprovalRuleCondition__c approvalRuleConditionTemplate, Integer count, Boolean isInsert) static List createApprovalRuleSecurities(enxCPQ__ApprovalRuleSecurity__c approvalRuleSecurityTemplate, Integer count, Boolean isInsert) static enxCPQ__ApprovalRuleSecurity__c createApprovalRuleSecurity(enxCPQ__ApprovalRuleSecurity__c approvalRuleSecurityTemplate, Boolean isInsert) static List createApprovalRules(enxCPQ__ApprovalRule__c approvalRuleTemplate, Integer count, Boolean isInsert) static enxCPQ__Attribute__c createAttribute(enxCPQ__Attribute__c attributeTemplate, Boolean isInsert) static enxCPQ__AttributeDefaultValue__c createAttributeDefaultValue(enxCPQ__AttributeDefaultValue__c attributeDefaultValueTemplate, Boolean isInsert) static List createAttributeDefaultValues(enxCPQ__AttributeDefaultValue__c attributeDefaultValueTemplate, Integer count, Boolean isInsert) static enxCPQ__AttributeRule__c createAttributeRule(enxCPQ__AttributeRule__c attributeRuleTemplate, Boolean isInsert) static List createAttributeRules(enxCPQ__AttributeRule__c attributeRuleTemplate, Integer count, Boolean isInsert) static enxCPQ__AttributeSet__c createAttributeSet(enxCPQ__AttributeSet__c attributeSetTemplate, Boolean isInsert) static enxCPQ__AttributeSetAttribute__c createAttributeSetAttribute(enxCPQ__AttributeSetAttribute__c attributeSetAttributeTemplate, Boolean isInsert) static List createAttributeSetAttributes(enxCPQ__AttributeSetAttribute__c attributeSetAttributeTemplate, Integer count, Boolean isInsert) static List createAttributeSets(enxCPQ__AttributeSet__c attributeSetTemplate, Integer count, Boolean isInsert) static enxCPQ__AttributeValue__c createAttributeValue(enxCPQ__AttributeValue__c attributeValueTemplate, Boolean isInsert) static List createAttributeValueDependencies(enxCPQ__AttributeValueDependency__c attributeValueDependencyTemplate, Integer count, Boolean isInsert) static enxCPQ__AttributeValueDependency__c createAttributeValueDependency(enxCPQ__AttributeValueDependency__c attributeValueDependencyTemplate, Boolean isInsert) static List createAttributeValues(enxCPQ__AttributeValue__c attributeValueTemplate, Integer count, Boolean isInsert) static List createAttributes(enxCPQ__Attribute__c attributeTemplate, Integer count, Boolean isInsert) static enxCPQ__BundleElement__c createBundleElement(enxCPQ__BundleElement__c bundleElementTemplate, Boolean isInsert) static enxCPQ__BundleElementOption__c createBundleElementOption(enxCPQ__BundleElementOption__c bundleElementOptionTemplate, Boolean isInsert) static List createBundleElementOptions(enxCPQ__BundleElementOption__c bundleElementOptionTemplate, Integer count, Boolean isInsert) static enxCPQ__BundleElementRule__c createBundleElementRule(enxCPQ__BundleElementRule__c bundleElementRuleTemplate, Boolean isInsert) static List createBundleElementRules(enxCPQ__BundleElementRule__c bundleElementRuleTemplate, Integer count, Boolean isInsert) static List createBundleElements(enxCPQ__BundleElement__c bundleElementTemplate, Integer count, Boolean isInsert) static enxCPQ__CPQ_Event__e createCPQEvent(enxCPQ__CPQ_Event__e eventTemplate, Boolean isInsert) static List createCPQEvents(enxCPQ__CPQ_Event__e eventTemplate, Integer count, Boolean isInsert) static enxCPQ__Cart__c createCart(enxCPQ__Cart__c cartTemplate, Boolean isInsert) static enxCPQ__Cart__c createCartItem(enxCPQ__Cart__c cartItemTemplate, Boolean isInsert) static List createCartItems(enxCPQ__Cart__c cartItemTemplate, Integer count, Boolean isInsert) static List createCarts(enxCPQ__Cart__c cartTemplate, Integer count, Boolean isInsert) static Case createCase(Case caseTemplate, Boolean isInsert) static List createCases(Case caseTemplate, Integer count, Boolean isInsert) static List createCategories(enxCPQ__Category__c categoryTemplate, Integer count, Boolean isInsert) static enxCPQ__Category__c createCategory(enxCPQ__Category__c categoryTemplate, Boolean isInsert) static Product2 createCharge(Product2 chargeTemplate, Boolean isInsert) static List createCharges(Product2 chargeTemplate, Integer count, Boolean isInsert) static Contact createContact(Contact contactTemplate, Boolean isInsert) static List createContacts(Contact contactTemplate, Integer count, Boolean isInsert) static Contract createContract(Contract contractTemplate, Boolean isInsert) static List createContracts(Contract contractTemplate, Integer count, Boolean isInsert) static List createDefaultCustomSettings(Boolean isInsert) static enxCPQ__Image__c createImage(enxCPQ__Image__c ImageTemplate, Boolean isInsert) static List createImages(enxCPQ__Image__c ImageTemplate, Integer count, Boolean isInsert) static Product2 createInactiveProduct(Product2 productTemplate, Boolean isInsert) static List createInactiveProducts(Product2 productTemplate, Integer count, Boolean isInsert) static List createOpportunities(Opportunity opportunityTemplate, Integer count, Boolean isInsert) static Opportunity createOpportunity(Opportunity opportunityTemplate, Boolean isInsert) static OpportunityLineItem createOpportunityLineItem(OpportunityLineItem itemTemplate, Boolean isInsert) static List createOpportunityLineItems(OpportunityLineItem itemTemplate, Integer count, Boolean isInsert) static Order createOrder(Order orderTemplate, Boolean isInsert) static OrderItem createOrderItem(OrderItem orderItemTemplate, Boolean isInsert) static enxCPQ__OrderItemAttribute__c createOrderItemAttribute(enxCPQ__OrderItemAttribute__c orderItemAttributeTemplate, Boolean isInsert) static List createOrderItemAttributes(enxCPQ__OrderItemAttribute__c orderItemAttributeTemplate, Integer count, Boolean isInsert) static List createOrderItems(OrderItem orderItemTemplate, Integer count, Boolean isInsert) static List createOrders(Order orderTemplate, Integer count, Boolean isInsert) static enxCPQ__PriceRule__c createPriceRule(enxCPQ__PriceRule__c priceRuleTemplate, Boolean isInsert) static enxCPQ__PriceRuleAction__c createPriceRuleAction(enxCPQ__PriceRuleAction__c priceRuleActionTemplate, Boolean isInsert) static List createPriceRuleActions(enxCPQ__PriceRuleAction__c priceRuleActionTemplate, Integer count, Boolean isInsert) static enxCPQ__PriceRuleCondition__c createPriceRuleCondition(enxCPQ__PriceRuleCondition__c priceRuleConditionTemplate, Boolean isInsert) static List createPriceRuleConditions(enxCPQ__PriceRuleCondition__c priceRuleConditionTemplate, Integer count, Boolean isInsert) static List createPriceRules(enxCPQ__PriceRule__c priceRuleTemplate, Integer count, Boolean isInsert) static Pricebook2 createPricebook(Pricebook2 pricebookTemplate, Boolean isInsert) static List createPricebookEntries(PricebookEntry pricebookEntryTemplate, Integer count, Boolean isInsert) static PricebookEntry createPricebookEntry(PricebookEntry pricebookEntryTemplate, Boolean isInsert) static List createPricebooks(Pricebook2 pricebookTemplate, Integer count, Boolean isInsert) static Product2 createProduct(Product2 productTemplate, Boolean isInsert) static enxCPQ__ProductAttribute__c createProductAttribute(enxCPQ__ProductAttribute__c productAttributeTemplate, Boolean isInsert) static List createProductAttributes(enxCPQ__ProductAttribute__c productAttributeTemplate, Integer count, Boolean isInsert) static enxCPQ__ProductGroup__c createProductGroup(enxCPQ__ProductGroup__c productGroupTemplate, Boolean isInsert) static enxCPQ__ProductGroupProduct__c createProductGroupProduct(enxCPQ__ProductGroupProduct__c productGroupProductTemplate, Boolean isInsert) static List createProductGroupProducts(enxCPQ__ProductGroupProduct__c productGroupProductTemplate, Integer count, Boolean isInsert) static List createProductGroups(enxCPQ__ProductGroup__c productGroupTemplate, Integer count, Boolean isInsert) static Product2 createProductOption(Product2 optionTemplate, Boolean isInsert) static List createProductOptions(Product2 optionTemplate, Integer count, Boolean isInsert) static enxCPQ__ProductRelationship__c createProductRelationship(enxCPQ__ProductRelationship__c productRelationshipTemplate, Boolean isInsert) static enxCPQ__ProductRelationshipRule__c createProductRelationshipRule(enxCPQ__ProductRelationshipRule__c productRelationshipRuleTemplate, Boolean isInsert) static List createProductRelationshipRules(enxCPQ__ProductRelationshipRule__c productRelationshipRuleTemplate, Integer count, Boolean isInsert) static List createProductRelationships(enxCPQ__ProductRelationship__c productRelationshipTemplate, Integer count, Boolean isInsert) static List createProducts(Product2 productTemplate, Integer count, Boolean isInsert) static Quote createQuote(Quote quoteTemplate, Boolean isInsert) static QuoteLineItem createQuoteLineItem(QuoteLineItem quoteItemTemplate, Boolean isInsert) static enxCPQ__QuoteLineItemAttribute__c createQuoteLineItemAttribute(enxCPQ__QuoteLineItemAttribute__c quoteItemAttributeTemplate, Boolean isInsert) static List createQuoteLineItemAttributes(enxCPQ__QuoteLineItemAttribute__c quoteItemAttributeTemplate, Integer count, Boolean isInsert) static List createQuoteLineItems(QuoteLineItem quoteItemTemplate, Integer count, Boolean isInsert) static List createQuotes(Quote quoteTemplate, Integer count, Boolean isInsert) static User createUser(String profileName, Boolean isInsert) static String generateRandomString(Integer len)

B2B_TestUtils

static Case createCase(Case caseTemplate, Boolean isInsert) static CaseComment createCaseComment(CaseComment caseCommentTemplate, Boolean isInsert) static List createCaseComments(CaseComment caseCommentTemplate, Integer count, Boolean isInsert) static List createCases(Case caseTemplate, Integer count, Boolean isInsert) static enxB2B__CostItem__c createCostItem(enxB2B__CostItem__c itemTemplate, Boolean isInsert) static enxB2B__Cost_Item_Bid__c createCostItemBid(enxB2B__Cost_Item_Bid__c costItemBidTemplate, Boolean isInsert) static List createCostItemBids(enxB2B__Cost_Item_Bid__c costItemBidTemplate, Integer count, Boolean isInsert) static List createCostItems(enxB2B__CostItem__c itemTemplate, Integer count, Boolean isInsert) static List createDefaultCustomSettings(Boolean isInsert) static enxB2B__POP__c createPOP(enxB2B__POP__c popTemplate, Boolean isInsert) static List createPOPs(enxB2B__POP__c popTemplate, Integer count, Boolean isInsert) static enxB2B__ProvisioningPlan__c createProvisioningPlan(enxB2B__ProvisioningPlan__c provPlanTemplate, Boolean isInsert) static enxB2B__ProvisioningPlanAssignment__c createProvisioningPlanAssignment(enxB2B__ProvisioningPlanAssignment__c provPlanAssTemplate, Boolean isInsert) static List createProvisioningPlanAssignments(enxB2B__ProvisioningPlanAssignment__c provPlanAssTemplate, Integer count, Boolean isInsert) static List createProvisioningPlans(enxB2B__ProvisioningPlan__c provPlanTemplate, Integer count, Boolean isInsert) static enxB2B__ProvisioningTask__c createProvisioningTask(enxB2B__ProvisioningTask__c provTaskTemplate, Boolean isInsert) static enxB2B__ProvisioningTaskAssignment__c createProvisioningTaskAssignment(enxB2B__ProvisioningTaskAssignment__c provTaskAssTemplate, Boolean isInsert) static List createProvisioningTaskAssignments(enxB2B__ProvisioningTaskAssignment__c provTaskAssTemplate, Integer count, Boolean isInsert) static List createProvisioningTasks(enxB2B__ProvisioningTask__c provTaskTemplate, Integer count, Boolean isInsert) static enxB2B__Service__c createService(enxB2B__Service__c serviceTemplate, Boolean isInsert) static enxB2B__ServiceAttribute__c createServiceAttribute(enxB2B__ServiceAttribute__c serviceAttributeTemplate, Boolean isInsert) static List createServicesAttributes(enxB2B__ServiceAttribute__c serviceAttributeTemplate, Integer count, Boolean isInsert) static List createServices(enxB2B__Service__c serviceTemplate, Integer count, Boolean isInsert) static WorkOrder createWorkOrder(WorkOrder workOrderTemplate, Boolean isInsert) static List createWorkOrders(WorkOrder workOrderTemplate, Integer count, Boolean isInsert)