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
Ignacio de la Torre 3Ignacio de la Torre 3 

How to properly test custom metadata in Apex ??

Hi all and thanks for reading!
I'm trying to test this class which uses some custom metadata and I'm having lots of trouble with it:
 
public class SendingSAPServices {
    
    public static final String SERVICE_SYNC_CLIENTE = 'SYNC_CLIENTE';
    public static final String SERVICE_SYNC_SEDE = 'SYNC_SEDE';
    public static final String SERVICE_SYNC_MATERIAL = 'SYNC_MATERIAL';
    public static final String SERVICE_SYNC_PEDIDO = 'SYNC_PEDIDO';
   
	public static void saveBy(List<Account> accounts){   
        
        List<SendingSAP__c> sendingsToInsert = new List<SendingSAP__c>();
        ConfiguracionServiceSAP__mdt confService = getConfiguracionService(SERVICE_SYNC_CLIENTE);
        //accounts.get(0).addError('error 1: ' + confService);
        if(confService != null){
            for(Account account : accounts){
                SendingSAP__c sending = getNew(confService, 'Account__c', account);
                sending.Request__c = SAPServices.getClienteSAPToSync(account);
                sendingsToInsert.add(sending);
            }
            insert sendingsToInsert;
            setLastSendingSAP(sendingsToInsert, accounts, 'Account__c');
        }
    }

This is the test class I've wrote so far:
 
@isTest
public class AccountTriggerHandlerTest {
    
    @testSetup public static void testSetup(){
        


        Account firstAccount = new Account();
        firstAccount.Name = 'First Account';
        firstAccount.SAPStatus__c = 'Pending SAP';
        firstAccount.CodigoCliente__c = '26000000';
        firstAccount.IsNew__c = true;
        insert firstAccount;
        
        Account secondAccount = new Account();
        secondAccount.Name = 'Second Account';
        secondAccount.SAPStatus__c = 'New';
        secondAccount.CodigoCliente__c = '66000000';
        secondAccount.IsNew__c = false;
        insert secondAccount;
    }
    
    @isTest public static void testAccount(){
        
        Test.startTest();
        
        ConfiguracionServiceSAP__mdt confService = [SELECT DeveloperName, MasterLabel, ServiceName__c 
                                                    FROM ConfiguracionServiceSAP__mdt 
                                                    WHERE ServiceName__c  = 'SYNC_CLIENTE' 
                                                    AND DeveloperName = 'DESARROLLO_SYN_CLIENTE'];
        
        Account firstAcc = [SELECT Name from Account WHERE Name = 'First Account'];
        Account secondAcc = [SELECT Name from Account WHERE Name = 'Second Account'];
        secondAcc.SAPStatus__c = 'Pending SAP';
        update secondAcc;
        
        Test.stopTest();

    }
}

This is the pictures of the Custom Metadata I'm using:

User-added imageUser-added image

but when I run the test the class get covered until here
 
if(confService != null){

and then it's all red ....

I need confService to be different to null so I can keep on testing but I don't know how to do this.
I've been googleing lots and lots and read heaps of documentation but I'm unable to solve it ... I'm new to Salesforce in general.

Any help would be more than appreciated.

Thanks for reading​​​​​​​
Danish HodaDanish Hoda
Hi There,
Custom Metadata types are queried from the org in test as well, so try to have such a combination where the returned metadata have some value other than null.
AnudeepAnudeep (Salesforce Developers) 
I recommend checking out Test Custom Metadata Types section in this Trailhead Module