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
Shubham Sinha 43Shubham Sinha 43 

How to write Test class for the apex code?

HI,
How to write test class for the below code. Please help me on this.
public class BIIB_ADU_ManageConsent_Controller{
    static Id recordTypeId = BIIB_Utility_Class.getRecordTypeId(BIIB_Adu_Constant.CONSENT_OBJECT, BIIB_Adu_Constant.CONSENT_RECORDTYPE );
    
    /**
* Method Name :     getConsentManagement
* @author:
* @description:     Aura Method to get consent management record 

**/
    @AuraEnabled
    public static List<BIIB_Consent_Management__c> getConsentManagement(){
        //Map<String,BIIB_Consent_Management__c> mapConsent = new Map<String,BIIB_Consent_Management__c>();
        User us = [Select id, accountId from User where Id = :UserInfo.getUserId()];
        List<BIIB_Consent_Management__c> consentList = new List<BIIB_Consent_Management__c>();
        system.debug ('User name' + us);
        ID AccountId = us.accountID;
        System.debug ('account name ' + AccountId );
        consentList= [SELECT ID,BIIB_Consent_Type__c,BIIB_SignatureFirstName__c,BIIB_SignatureLastName__c,BIIB_Status__c,BIIB_Effective_Date__c,CreatedDate 
                      FROM BIIB_Consent_Management__c WHERE 
                      BIIB_Patient_Account__c =: AccountId AND
                      BIIB_Status__c = :BIIB_Adu_Constant.CONSENT_STATUS AND
                      BIIB_Consent_Flag__c = true AND
                      RecordTypeId = :recordTypeId AND
                      (BIIB_Consent_Type__c = :BIIB_Adu_Constant.CONSENT_TYPE_HIPAA OR
                       BIIB_Consent_Type__c = :BIIB_Adu_Constant.CONSENT_TYPE_PATIENTSERVICES) 
                     LIMIT 2];
        
        Map<String,BIIB_Consent_Management__c> mapConsent = new Map<String,BIIB_Consent_Management__c>();
        for(BIIB_Consent_Management__c cm:consentList){
            mapConsent.put(cm.BIIB_Consent_Type__c,cm);
        }
        if(!mapConsent.containsKey(BIIB_Adu_Constant.CONSENT_TYPE_HIPAA)){
            consentList.add(new BIIB_Consent_Management__c(BIIB_Consent_Type__c=BIIB_Adu_Constant.CONSENT_TYPE_HIPAA));
        }
        if(!mapConsent.containsKey(BIIB_Adu_Constant.CONSENT_TYPE_PATIENTSERVICES)){
            consentList.add(new BIIB_Consent_Management__c(BIIB_Consent_Type__c=BIIB_Adu_Constant.CONSENT_TYPE_PATIENTSERVICES));
        }
        return consentList;
    }
    
    
}
SwethaSwetha (Salesforce Developers) 
HI Shubham,
The below articles give you a basic idea of how test classes are to be written and how to improve the code coverage. You will need to follow the best practices and write your custom code.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_intro

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_best_practices.htm

https://help.salesforce.com/articleView?id=000335222&language=en_US&type=1&mode=1

https://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/

Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.

Thank you