0

I am using trying to stub my class, but I have get and set for custom metadata. I know that I cannot stub get and set. Is there exist any design for using it in stub? Or I must just hardcode it and create a list (or something else) with custom metadata objects? Google didn't help find the answer

Sorry have problems with net and cannot update. Here is my get and set for custom metadata

@testVisible  List<Configuration__mdt> configuration{

    get{
        if(configuration == NULL){
            configuration = [SELECT Object__c, queryCondition__c FROM Configuration__mdt];
        }
        return configuration;
    }
    set;
}
@testVisible  List<ConfigFields__mdt> configFields{
    get{
        if(configFields == NULL){
            configFields = [SELECT fieldName__c FROM         ConfigFields__mdt];
        }
        return configFields;
    }
    set;
}
6
  • Custom Metadata can be queried from Org in test class even with SeeAllData=false. So you may not need a stub api for that. Sep 10 at 6:11
  • can you provide your code, which you are trying to stub ? {get;set;} properties can always be replaced with getter and setter method. Sep 10 at 6:12
  • Hi. Please add samples of your code because it is not clear what you mean by "get and set" for custom metadata. Do you mean use of metadata API? Do you mean use of get Instance? Please edit to add details (do not reply in a comment).
    – Phil W
    Sep 10 at 6:24
  • 1
    BTW a typical option is to ensure you provide a separate class that provides the API through which you update and retrieve custom metadata type records then stub that class. You can create in-memory custom metadata type records via JSON.deserialize without any restriction.
    – Phil W
    Sep 10 at 6:42
  • @PhilW thanks. I think i understand. Added code if needed.
    – lemleborze
    Sep 10 at 8:38
1

in this case, you can replace get and set properties (as it is not possible to mock properties with Stub API) with the getter and the setter method, and you can mock them.

private List<Configuration__mdt> configuration;
private List<ConfigFields__mdt> configFields;

public List<Configuration__mdt> getConfiguration(){
    if(configuration == NULL){
        configuration = [SELECT Object__c, queryCondition__c FROM Configuration__mdt];
    }
    return configuration;
}

public void setConfiguration(List<ConfigFields__mdt> configuration){
    this.configuration = configuration;
}

public List<ConfigFields__mdt> getConfigFields(){
    if(configFields == NULL){
        configFields = [SELECT fieldName__c FROM ConfigFields__mdt];
    }
    return configFields;
}

public void setConfigFields(List<ConfigFields__mdt> configFields){
    this.configFields = configFields;
}
3
  • So i must just create list of mdt in test class and pass to setConfigFields/setConfiguration? Thanks.
    – lemleborze
    Sep 10 at 11:36
  • and you are mocking with those lists getConfiguration and getConfigFields methods Sep 10 at 11:37
  • Got it. I thought about another method but no clues.
    – lemleborze
    Sep 10 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.