function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rohith Kumar 98Rohith Kumar 98 

How to attach test classes to mocking framework?

how to attach this test class to the mocking framework? and what is its use?
@isTest
public class PaymentOrderTriggerHandlerTest {
    @isTest public static void testUpdatePaymentContactInformationCheck(){
        //Creating account
        Account account = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street',
                                      ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State',
                                      ShippingCountry = 'Test country');
        insert account;
        
        //Creating Payment Order with above account as lookup
        Payment_Order__c paymentOrder = new Payment_Order__c(Account__c = account.Id);
        insert paymentOrder;
        
        Test.startTest();
        //Getting all test data
        String paymentOrderName = [SELECT Payee_Name__c FROM Payment_Order__c WHERE Account__c = :account.Id].Payee_Name__c;
        String paymentOrderPhone = [SELECT Phone_No__c FROM Payment_Order__c WHERE Account__c = :account.Id].Phone_No__c;
        String paymentOrderStreet1 = [SELECT Address_Street_1__c FROM Payment_Order__c WHERE Account__c = :account.Id].Address_Street_1__c;
        String paymentOrderStreet2 = [SELECT Address_Street_2__c FROM Payment_Order__c WHERE Account__c = :account.Id].Address_Street_2__c;
        String paymentOrderZip = [SELECT Zip__c FROM Payment_Order__c WHERE Account__c = :account.Id].Zip__c;
        String paymentOrderState = [SELECT State__c FROM Payment_Order__c WHERE Account__c = :account.Id].State__c;
        String paymentOrderCountry = [SELECT Country__c FROM Payment_Order__c WHERE Account__c = :account.Id].Country__c;
        
        System.assertEquals(account.Name, paymentOrderName);
        System.assertEquals(account.Phone, paymentOrderPhone);
        System.assertEquals(account.BillingStreet, paymentOrderStreet1);
        System.assertEquals(account.ShippingStreet, paymentOrderStreet2);
        System.assertEquals(account.ShippingPostalCode, paymentOrderZip);
        System.assertEquals(account.ShippingState, paymentOrderState);
        System.assertEquals(account.ShippingCountry, paymentOrderCountry);
        Test.stopTest();
    }
    
    @isTest public static void testInsertAccountContactInformationCheck(){
        //Creating Payment Order without lookup account
        Payment_Order__c paymentOrder = new Payment_Order__c(Payee_Name__c = 'Test Payment Order', Phone_No__c = '9876543210', 
                                                             Address_Street_1__c = 'Test Street 1', Address_Street_2__c = 'Test Street 2',
                                                             Zip__c = '625014', State__c = 'Test State', Country__c = 'Test Country');
        insert paymentOrder;
        
        //Getting Id, all data of account created by trigger
        ID accountID = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c;
        String accountName = [SELECT Name FROM Account WHERE Id = :accountId].Name;
        String accountPhone = [SELECT Phone FROM Account WHERE Id = :accountId].Phone;
        String accountBillingStreet = [SELECT BillingStreet FROM Account WHERE Id = :accountId].BillingStreet;
        String accountShippingStreet = [SELECT ShippingStreet FROM Account WHERE Id = :accountId].ShippingStreet;
        String accountZip = [SELECT ShippingPostalCode FROM Account WHERE Id = :accountId].ShippingPostalCode;
        String accountState = [SELECT ShippingState FROM Account WHERE Id = :accountId].ShippingState;
        String accountCountry = [SELECT ShippingCountry FROM Account WHERE Id = :accountId].ShippingCountry;
        
        Test.startTest();
        System.assertEquals(paymentOrder.Payee_Name__c, accountName);
        System.assertEquals(paymentOrder.Phone_No__c, accountPhone);
        System.assertEquals(paymentOrder.Address_Street_1__c, accountBillingStreet);
        System.assertEquals(paymentOrder.Address_Street_2__c, accountShippingStreet);
        System.assertEquals(paymentOrder.Zip__c, accountZip);
        System.assertEquals(paymentOrder.State__c, accountState);
        System.assertEquals(paymentOrder.Country__c, accountCountry);
        Test.stopTest();
    }
    
    @isTest public static void testSearchAndCreateAccountCheck(){
        //Creating Account
        Account account = new Account(Name = 'Test Account', Phone = '1234567890', BillingStreet = 'Test Billing Street',
                                      ShippingStreet = 'Test Shipping Street', ShippingPostalCode = '625014', ShippingState = 'Test State',
                                      ShippingCountry = 'Test country');
        insert account;
        
        //creating Payment Order
        Payment_Order__c paymentOrder = new Payment_Order__c(Payee_Name__c = 'Test Payment Order', Phone_No__c = '9876543210', 
                                                             Address_Street_1__c = 'Test Street 1', Address_Street_2__c = 'Test Street 2',
                                                             Zip__c = '625014', State__c = 'Test State', Country__c = 'Test Country');
        insert paymentOrder;
        
        //updating payment order to match above account data
        paymentOrder.Payee_Name__c = 'Test Account';
        paymentOrder.Phone_No__c = '1234567890';
        paymentOrder.Address_Street_1__c = 'Test Billing Street';
        paymentOrder.Address_Street_2__c = 'Test Shipping Street';
        paymentOrder.Zip__c = '625014';
        paymentOrder.State__c = 'Test State';
        paymentOrder.Country__c = 'Test Country';
        update paymentOrder;
        
        //getting id of account associated with payment order
        ID paymentOrderAccountId = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c;
                
        Test.startTest();
        System.assertEquals(account.Id, paymentOrderAccountId);
        
        //updating payment order to not match above account data
        paymentOrder.Phone_No__c = '7777777777';
        update paymentOrder;
        
        //getting updated account lookup id
        paymentOrderAccountId = [SELECT Account__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Account__c;
        
        System.assertNotEquals(account.Id, paymentOrderAccountId);
        Test.stopTest();
    }
    
    @isTest public static void testUpdateStatusAndOwnerCheck(){
        //Creating Payment Order with payment amount greater than 2000
        Payment_Order__c paymentOrder = new Payment_Order__c(Payment_Amount__c = 2001);
        insert paymentOrder;
        
        //Getting status of payment order
        String status = [SELECT Status__c FROM Payment_Order__c WHERE Id = :paymentOrder.Id].Status__c;
        
        //getting Queue Id
        ID queueId = [SELECT Queue.Id FROM queuesobject WHERE queue.name='Payment Order Pending Approval'].Queue.Id;
        ID paymentOrderOwnerId = [Select OwnerId FROM Payment_Order__c WHERE Id = :paymentOrder.Id].OwnerId;
        
        Test.startTest();
        System.assertEquals('Pending Approval', status);
        System.assertEquals(queueId, paymentOrderOwnerId);
        Test.stopTest();
    }
}

 
Best Answer chosen by Rohith Kumar 98
Suraj Tripathi 47Suraj Tripathi 47
Hi,

Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies. In a mocking, the dependencies are replaced by closely controlled replacements objects that simulate the behavior of the real ones. There are three main possible types of replacement objects - fakes, stubs, and mocks.
Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks. By isolating the dependencies, they help the unit testing process and aid developers in writing more focused and concise unit tests. The tests also perform faster by truly isolating the system under test.

Follow the below link hope it will help you.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_stub_api.htm