You need to sign in to do that
Don't have an account?
Ignacio 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:
This is the test class I've wrote so far:
This is the pictures of the Custom Metadata I'm using:
but when I run the test the class get covered until here
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
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:
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
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.