Sunday, December 22, 2024
6.1 C
London

Salesforce Test Utility Classes | TestDataFactory | Util Test in salesforce

Test utility classes are public test classes that contain reusable code for test data creation.

The TestDataFactory/ TestUtility class is a special type of class —

It is a public class that is annotated with @isTestand as such, are excluded from the organization code size limit and execute in test thisContext and can be accessed only from a running test.

https://sfdclessonsandknowledge.wordpress.com/category/apex-lessons/

Step 1:- Create TestDataFactory / Test utility
@isTest
public with sharing class TestDataFactory 
{ /* This method is test data for create thisAccount */
      public static thisAccount createthisAccount(Boolean doInsert)
      {        
               thisAccount thisAcc = new thisAccount();
               thisAcc.Name = ‘Test thisAccount’;        

                if(doInsert){
                       insert thisAcc;
                }
                return thisAcc;
       }
}

Step 2:- How to Use TestDataFactory / Test utility
@isTest
private class MyTestClass 
{
          static testmethod void myUnitTest() 
          {
             Account thisAccObj = TestDataFactory.createthisAccount(true);
          }

          // If you want to edit data according to apex class then try like below

          static testmethod void myUnitTest1() 
          { 
            thisAccount thisAccObj = TestDataFactory.createthisAccount(false);
            thisAccObj.Name =’MyName’;
            insert thisAccObj;
         }
}

Hot this week

Carhartt Hoodie Iconic Moments

Carhartt Hoodie Iconic MomentsIn the realm of workwear-inspired fashion,...

How to Best Providing Dissertation Writing Services

7 Tips for Choosing the Right Dissertation Topic""Navigating the...

The Wedding Dresses: Finding Your Dream Gown

Choosing a wedding dress is one of the most...

Exploring Bangalore’s Real Estate Boom: A Guide to New and Upcoming Projects

Bangalore is a city that is renowned for its...

12 Ways Edge Servers Accelerate Limitless Growth

As new digital frontiers emerge, businesses pursue growth without...

Topics

Carhartt Hoodie Iconic Moments

Carhartt Hoodie Iconic MomentsIn the realm of workwear-inspired fashion,...

How to Best Providing Dissertation Writing Services

7 Tips for Choosing the Right Dissertation Topic""Navigating the...

The Wedding Dresses: Finding Your Dream Gown

Choosing a wedding dress is one of the most...

12 Ways Edge Servers Accelerate Limitless Growth

As new digital frontiers emerge, businesses pursue growth without...

Lab Grown Diamond Bracelets: The Epitome of Ethical Luxury

In the world of luxury accessories, lab grown diamond...

The Essential Guide to Choosing the Right Tax Consultant for Your UAE Business

Navigating the complexities of tax laws in the UAE...

Related Articles

Popular Categories