Test Data Factory in Apex Salesforce

by Rijwan Mohmmed
0 comment
test-data-factory-in-apex-salesforce

Hello, friends today we are going to discuss Test Data Factory in Apex Salesforce. In this Test Data Factory class, we will create reusable methods for creating data that we can use in our test class.

Also, check this: Get Current User Info in LWC

Key Highlights :

  1. Use Test Data Factory / testutility classes to add reusable methods for test data setup.
  2. Create static methods in this class.
  3. It is a public class that is annotated with @isTest.
  4. We can use and call these class methods in multiple test classes.
  5. We will create test data in testutility class and use these data in test class.

Code :

TestDataFactory.cls :

@isTest
public class TestDataFactory {
    public static Id createAccount(){
        Account testAcc = new Account(
            Name = 'Test Account',
            Type = 'Site'
        );
        insert testAcc;
        
        return testAcc.Id;
    }

    public static Id createContractForAccount(Id accId, String emailId){
        Contact con = new Contact();
        con.FirstName = 'First Test';
        con.LastName = 'Last Test';
        con.Email = emailId;
        con.Phone = '999-999-9999';
        con.AccountId = accId;
        con.MailingStreet = '123 Test Avenue';
        con.MailingCity = 'Test';
        con.MailingState = 'Florida';
        con.MailingPostalCode = '30010';
        con.MailingCountry = 'United States';
        insert con;
        
        return con.Id;
    }

    public static Id createContactForAccount(Id accId, String emailId){
        Contact con = new Contact();
        con.FirstName = 'First Test';
        con.LastName = 'Last Test';
        con.Email = emailId;
        con.Phone = '999-999-9999';
        con.AccountId = accId;
        con.MailingStreet = '123 Test Avenue';
        con.MailingCity = 'Test';
        con.MailingState = 'Florida';
        con.MailingPostalCode = '30010';
        con.MailingCountry = 'United States';
        insert con;
        
        return con.Id;
    }

    public static Id createOpportunityForAccount(Id accId, String Stagename){
        Opportunity opp = new Opportunity();
        opp.Name ='Test'; 
        opp.AccountID = accId;
        opp.Type = 'New';
        opp.CloseDate = System.today();
        opp.StageName = Stagename;
        
        insert opp;
        
        return opp.Id;
    }

    public static Id createProduct2(){
        Product2 prod = new Product2(
            Name = 'Product X',
            ProductCode = 'Pro-X',
            isActive = true
        );
        
        insert prod;
        
        return prod.Id;
    }

    public static Id createPricebookEntry(Id productId){
        PricebookEntry standardPriceEntry = new PricebookEntry();
        standardPriceEntry.IsActive = true;
        standardPriceEntry.Product2Id = productId;
        standardPriceEntry.Pricebook2Id = Test.getStandardPricebookId();
        standardPriceEntry.UnitPrice = 10;
        standardPriceEntry.UseStandardPrice = false;
        insert standardPriceEntry;
        
        return standardPriceEntry.Id;
    }

    public static Id createOppLineItem(Id oppId, Id priceBookEntryId, String oppStatus){
        OpportunityLineItem  oppLineItem = new OpportunityLineItem();
        oppLineItem.OpportunityId = oppId;
        oppLineItem.Quantity = 1;
        oppLineItem.PricebookEntryId = priceBookEntryId;
        oppLineItem.TotalPrice = 100.00;
        insert oppLineItem;
        
        return oppLineItem.Id;
    }


    public static void createTestOpportunityContactRole(ID oppId, ID conId){
        OpportunityContactRole ContactRoleObj = new OpportunityContactRole();
        ContactRoleObj.ContactId = conId;
        ContactRoleObj.IsPrimary = true;
        ContactRoleObj.OpportunityId = oppId;
        
        insert ContactRoleObj;
    }

    public static void createTestOrder(ID accId){
        Order order = new Order(
            AccountId = accId,
            EffectiveDate = System.today(),
            Status = 'Draft',
            PriceBook2Id = Test.getStandardPricebookId()
        );
        insert order;
    }

    public static void createTestOrderLine(ID orderId, Id product2Id, Id pricebookEntryId){
        OrderItem lineItem = new OrderItem();
        lineItem.OrderId = orderId;
        lineItem.Quantity = 10;
        lineItem.UnitPrice = 240;
        lineItem.Product2id = product2Id;
        lineItem.PricebookEntryId = pricebookEntryId;
        insert lineItem;

        // Now update & Activate the Order
        Order order = new Order();
        order.Id = orderId;
        order.Status = 'Activated';
        update order;
    }
}

OrderTest.cls :

@isTest
public class OrderTest{

   @isTest 
   public void myUnitTest() {
        Account acc = TestDataFactory.createAccount();
        Contact cont = TestDataFactory.createContactForAccount(acc.id, 'test@g.com');
        Product2 pro = TestDataFactory.createProduct2();
        PricebookEntry standardPriceEntry = TestDataFactory.createPricebookEntry(pro.Id);
        Order order = TestDataFactory.createTestOrder(acc.Id);
        OrderItem lineItem = TestDataFactory.createTestOrderLine(order.Id, pro.Id, standardPriceEntry.Id);
   }

}

Reference :

  1. Create Test Data for Apex Tests

What’s your Reaction?
+1
2
+1
0
+1
3
+1
0
+1
0
+1
0

You may also like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.