+ Start a Discussion
Shubham Sinha 49Shubham Sinha 49 

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;
    }
    
    
}

 
Darshan Gandhi 9Darshan Gandhi 9

Hi  Shubham Sinha 49

You can try like this for writing basic test class and then try to cover the test code coverage by

 

Let me know if you need further help and also 

Select this as the best answer if it helped you


@isTest(SeeAllData= true)

public class BIIB_ADU_ManageConsent_ControllerTest {
    @isTest static void getConsentManagementTest()
    {
        List<BIIB_Consent_Management__c> testList = BIIB_ADU_ManageConsent_Controller.getConsentManagement();
    }
}

sachinarorasfsachinarorasf
Hi Shubham Sinha,

I have gone through your problem. 

Here is the code you can use and update as per your requirements.

@isTest
private class BIIB_ADU_ManageConsent_Controller_Test {

    @isTest static void getConsentManagement_Test() {
        
        Profile pf= [Select Id from profile where Name='System Administrator'];
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User createUserObject = new User(firstname = 'ABC', 
                                         lastName = 'XYZ', 
                                         email = uniqueName + '@test' + orgId + '.org', 
                                         Username = uniqueName + '@test' + orgId + '.org', 
                                         EmailEncodingKey = 'ISO-8859-1', 
                                         Alias = uniqueName.substring(18, 23), 
                                         TimeZoneSidKey = 'America/Los_Angeles', 
                                         LocaleSidKey = 'en_US', 
                                         LanguageLocaleKey = 'en_US', 
                                         ProfileId = pf.Id
                                        ); 
        insert createUserObject;
        
        BIIB_Consent_Management__c consentManagementObject = new BIIB_Consent_Management__c();
        consentManagementObject.BIIB_Consent_Type__c = '';
        consentManagementObject.BIIB_SignatureFirstName__c = '';
        consentManagementObject.BIIB_SignatureLastName__c = '';
        consentManagementObject.BIIB_Status__c = '';
        consentManagementObject.BIIB_Effective_Date__c = '';
        consentManagementObject.BIIB_Patient_Account__c = createUserObject; 
        insert consentManagementObject;
        
        
        Test.startTest();
        BIIB_ADU_ManageConsent_Controller.getConsentManagement_Test();
        Test.stopTest();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora