0

I Have a map of custom metadata type Map<String,error_mdt> error

error=[select id, DeveloperName from error_mdt].How can we Use meta data types in test class to get code coverage.

I have already checked on the solution given my salesforce creating testcase in metadata type but Is there any other solution we can have without creating TestCase__C field.

0

1 Answer 1

0

There is possible hack to have different values in tests than it is in organization.

This is a good way to have your tests always stable and 100% covered even if custom metadata is changed or removed on your organization.

For this hack you just need to put query for custom metadata into separate property which should be settable for unit tests.

@testVisible static List<Custom_Metadata__mdt> customMetadata { 
    get {
        if ( customMetadata == null )
            customMetadata = [ SELECT All_Needed_Custom_Fields__c FROM Custom_Metadata__mdt ]; 
        return customMetadata;
    } set; }

Then you can use JSON.deserialize method to setup these metadata records. This method allows you to overcome 'field is not writable' error.

Example of code you could use is the following:

CustomClass.customMetadata  = (List<Custom_Metadata__mdt>) JSON.deserialize( '{"All_Needed_Custom_Fields__c ":"dummyValue"}', List<Custom_Metadata__mdt>.class );

Hopefully this hack will be available in the future as well, since this is the only way to get coverage for code.

This is based on the comment on Idea Exchange - Custom MetaData Types need APEX methods to access/update

0

Not the answer you're looking for? Browse other questions tagged .