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
kailash chandrakailash chandra 

create test class to insert metadata record

Hello,
I need the 100% code coverage of this code, Where I am retrieving records from custom metadata. So I need to insert metadata through test class.
Please help me.
public class GenerateAccess{
    public Map<String,String> revokeAccess(String realmId){
        Map<String,String> rtrnMap=new Map<String,String>() ;
        String errorString='';    
        List<Token__mdt> tokens=new List<Token__mdt>([Select DeveloperName,MasterLabel,Access_Token__c,Sandbox_Client_Id__c,Sandbox_Client_Secret__c,Refresh_Token_Expires_in__c,Realm_Id2__c from Token__mdt where Realm_Id2__c=:realmId Limit 1]);
        if(tokens.size()>0){
            String encodedString = EncodingUtil.base64Encode(Blob.valueOf(to[0].Sandbox_Client_Id__c+':'+tokens[0].Sandbox_Client_Secret__c));
        }
    }
}
AnudeepAnudeep (Salesforce Developers) 
Hi Kailash, 

The examples in the following blogs might help

https://www.forcetalks.com/blog/custom-metadata-test-class-coverage-in-salesforce/

http://www.infallibletechie.com/2018/12/code-coverage-for-custom-metadata-type.html

Also if you want to better understand how custom metadata types behave in Apex tests refer to Testing Custom Metadata Types

Anudeep