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
Procurement ExecutiveProcurement Executive 

Test class for the following code

public with sharing class CancelMembership_ctrl {
    @AuraEnabled
    public Static Account cancelMembership(String accountId){
        try {
            Account objAccount = [SELECT Id,Membership_Level__c,Membership_Type__c,Home_Clinic__c,
                                    Patient_Type__c,Membership_Add_On__c,
                                    Member_Status__c,Cancellation_Date__c, Signature_Date__c
                                    FROM Account
                                    WHERE Id = :accountId LIMIT 1];
            objAccount.Cancelled_membership__c = true;
            objAccount.Patient_Type__c = 'Registered';
            objAccount.Membership_Type__c = null;
            objAccount.Membership_Level__c = null;
            objAccount.Membership_Add_On__c = null;
            objAccount.Member_Status__c = 'Membership Cancelled';
            objAccount.Cancellation_Date__c = system.Today();
            update objAccount;
            return objAccount;
        }catch(Exception e){
            throw e;
        }
    }
    @AuraEnabled 
    public static List < String > getPicklistValue() {
        List < String > options = new List < String > ();
        Schema.DescribeFieldResult fieldResult = Account.Cancellation_Reason__c.getDescribe();
        List < Schema.PicklistEntry > ple = fieldResult.getPicklistValues();
        for (Schema.PicklistEntry f: ple) {
            options.add(f.getLabel());
        }
        return options;
    }
}
Procurement ExecutiveProcurement Executive
Test Class :-

@isTest
private class CancelMembership_ctrl_Test{  
    @testSetup 
    public static void setUpMockData(){
           Account acc = new Account(
           FirstName='Test FName',
           LastName='Test LName',   
           PersonMailingStreet='test@yahoo.com',
           PersonMailingPostalCode='12345',
           PersonMailingCity='SFO',
           PersonEmail='test@yahoo.com',
           PersonHomePhone='1234567',
           PersonMobilePhone='12345678',
           Relation_With_Payer__c='Brother',
           Cancelled_membership__c = true,
           Patient_Type__c = 'Member',
           Membership_Type__c = '2019 MHC GP',
           Membership_Level__c = 'Adult - Main Payer',
           Membership_Add_On__c = 'Healthscreen',
           Member_Status__c = 'Member',
           Cancellation_Date__c = system.Today(),
           Signature_Date__c = system.Today(),
           Cancellation_Reason__c= 'Died',
           Date_of_Birth__c = system.Today(),
           Gender__c = 'Male',
           Home_Clinic__c='Wandsworth' 
       ); 
       insert acc; 
           acc.Cancelled_membership__c = true;
           acc.Patient_Type__c = 'Registered';
           acc.Membership_Type__c = null;
           acc.Membership_Level__c = null;
           acc.Membership_Add_On__c = null;
           acc.Member_Status__c = 'Membership Cancelled';
           acc.Cancellation_Date__c = system.Today();
           acc.Signature_Date__c = system.Today();
           acc.Cancellation_Reason__c= 'Moving Away';
           acc.Home_Clinic__c='Wandsworth';
           Update acc;
       }
       
       CancelMembership_ctrl cancelM = new CancelMembership_ctrl();
      // cancelM.cancelMembership(acc.Id);
       
       //Test.startTest();
       //List < String > options = CancelMembership_ctrl.getPicklistValue();
       //Test.stopTest();
       //system.assertNotEquals(null, options, 'The collection should be instantiated');
       //system.assertEquals(options.get(0).getvalue(),'The collection should be instantiated');
      // system.assert(!options.isEmpty(),'The collection should be populated');
   
       }
Ajay K DubediAjay K Dubedi
Hi,
Try this code for test class:
@isTest
private class CancelMembership_ctrl_Test {
    @isTest static void testCancelMembership() {
        Account accObj = new Account();
        accObj.Name = 'Test';
        accObj.Membership_Level__c = 'Test';
        accObj.Membership_Type__c = 'Test';
        accObj.Home_Clinic__c = 'Test';
        accObj.Patient_Type__c = 'Registered';
        accObj.Membership_Add_On__c = 'Test';
        accObj.Member_Status__c = 'Test';
        accObj.Cancellation_Date__c = date.today();
        accObj.Signature_Date__c = date.today();
        insert accObj;
        System.assertNotEquals(accObj.Id, null,'Assertion Failed : Account Not Inserted');
        
        test.startTest();
        CancelMembership_ctrl.cancelMembership(accObj.Id);
        CancelMembership_ctrl.getPicklistValue();
        test.stopTest();
    }
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Procurement ExecutiveProcurement Executive
Hello @Ajay K Dubedi , Thanks for the revert.

But I am still struck with this test class as it is giving me error for restricted picklist. Like Home Clinic I am providing exact data as per the picklist values for the Custom Field.
Procurement ExecutiveProcurement Executive
I think I have data flow issue for account object for insert and update