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
Soundar Rajan PonpandiSoundar Rajan Ponpandi 

How to cover a test class for this sample class ?

Hi,

Can anyone please help to cover a test class for following simple class.
 
public without sharing class GD_CartShare {

    public static void shareCartRecordWhilePlacingOrder(List<GD_Cart__c> cartItems,List<User> approverUserList ){
       
    }
    
    public static void deleteCartItems(List<GD_Cart__c> toBeDeletedCart){
        Delete toBeDeletedCart;
    }
    
}

Regards,
Soundar.
VinodBatraVinodBatra
Hi Soundar,

You can take the reference from below test class.
I have not compiled it. You can modify the class as per your need.
@isTest
private class GD_CartShare{
	
	@testSetup
	static void dataSetup(){
	
		// Create Record for User of profile you want
		User objUser = TestFactory.createUser('System Administrator', true); //Create a class TestFactory with createUser Method
		
		System.runAs(objUser){
			List<GD_Cart__c> lstGDCart = TestFactory.createGDCart(5, true); ////Create a Method in TestFactory to create n number of GD_Cart__c records according to parameter
			
		}
	}
	
	@isTest
	private static void shareCartRecordWhilePlacingOrderTest(){
		List<User> lstUser  = [Select Id FROM User];
		List<GD_Cart__c> lstGDCart;
		Boolean blnExFlag;
		System.runAs(lstUser[0]){
			try{
				blnExFlag = false;
				lstGDCart = [Select Id from GD_Cart__c];
				GD_CartShare.shareCartRecordWhilePlacingOrder(lstGDCart, lstUser);
			}catch(Exception ex){
				blnExFlag = true;
			}
			// To ensure that method excuted without any exception
			System.assertEquals(blnExFlag, false);
		}
	}
	
	@isTest
	private static void deleteCartItemsTest(){
		List<User> lstUser  = [Select Id FROM User];
		List<GD_Cart__c> lstGDCart = new List<GD_Cart__c>();
		List<GD_Cart__c> lstGDCartAfterDelete = new List<GD_Cart__c>();
		
		System.runAs(lstUser[0]){
			
			blnExFlag = false;
			lstGDCart = [Select Id from GD_Cart__c];
			GD_CartShare.deleteCartItems(lstGDCart);
			lstGDCartAfterDelete = [Select Id from GD_Cart__c];
			
			// Ensures that GD_Cart__c records are deleted
			System.assertEquals(lstGDCartAfterDelete.size(), 0);
		}
	}
}

Thanks,
Vinod